Search in sources :

Example 51 with MessageDigest

use of java.security.MessageDigest in project cube-sdk by liaohuqiu.

the class Encrypt method md5.

/**
     * A hashing method that changes a string (like a URL) into a hash string
     */
public static String md5(String key) {
    String cacheKey;
    try {
        final MessageDigest mDigest = MessageDigest.getInstance("MD5");
        mDigest.update(key.getBytes());
        cacheKey = bytesToHexString(mDigest.digest());
    } catch (NoSuchAlgorithmException e) {
        cacheKey = String.valueOf(key.hashCode());
    }
    return cacheKey;
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 52 with MessageDigest

use of java.security.MessageDigest in project libgdx by libgdx.

the class GlyphPage method renderGlyph.

/** Loads a single glyph to the backing texture, if it fits. */
private boolean renderGlyph(Glyph glyph, int pageX, int pageY, int width, int height) {
    scratchGraphics.setComposite(AlphaComposite.Clear);
    scratchGraphics.fillRect(0, 0, MAX_GLYPH_SIZE, MAX_GLYPH_SIZE);
    scratchGraphics.setComposite(AlphaComposite.SrcOver);
    ByteBuffer glyphPixels = scratchByteBuffer;
    int format;
    if (unicodeFont.getRenderType() == RenderType.FreeType && unicodeFont.bitmapFont != null) {
        BitmapFontData data = unicodeFont.bitmapFont.getData();
        BitmapFont.Glyph g = data.getGlyph((char) glyph.getCodePoint());
        Pixmap fontPixmap = unicodeFont.bitmapFont.getRegions().get(g.page).getTexture().getTextureData().consumePixmap();
        int fontWidth = fontPixmap.getWidth();
        int padTop = unicodeFont.getPaddingTop(), padBottom = unicodeFont.getPaddingBottom();
        int padLeftBytes = unicodeFont.getPaddingLeft() * 4;
        int padXBytes = padLeftBytes + unicodeFont.getPaddingRight() * 4;
        int glyphRowBytes = width * 4, fontRowBytes = g.width * 4;
        ByteBuffer fontPixels = fontPixmap.getPixels();
        byte[] row = new byte[glyphRowBytes];
        glyphPixels.position(0);
        for (int i = 0; i < padTop; i++) glyphPixels.put(row);
        glyphPixels.position((height - padBottom) * glyphRowBytes);
        for (int i = 0; i < padBottom; i++) glyphPixels.put(row);
        glyphPixels.position(padTop * glyphRowBytes);
        for (int y = 0, n = g.height; y < n; y++) {
            fontPixels.position(((g.srcY + y) * fontWidth + g.srcX) * 4);
            fontPixels.get(row, padLeftBytes, fontRowBytes);
            glyphPixels.put(row);
        }
        fontPixels.position(0);
        glyphPixels.position(height * glyphRowBytes);
        glyphPixels.flip();
        format = GL11.GL_RGBA;
    } else {
        // Draw the glyph to the scratch image using Java2D.
        if (unicodeFont.getRenderType() == RenderType.Native) {
            for (Iterator iter = unicodeFont.getEffects().iterator(); iter.hasNext(); ) {
                Effect effect = (Effect) iter.next();
                if (effect instanceof ColorEffect)
                    scratchGraphics.setColor(((ColorEffect) effect).getColor());
            }
            scratchGraphics.setColor(java.awt.Color.white);
            scratchGraphics.setFont(unicodeFont.getFont());
            scratchGraphics.drawString("" + (char) glyph.getCodePoint(), 0, unicodeFont.getAscent());
        } else if (unicodeFont.getRenderType() == RenderType.Java) {
            scratchGraphics.setColor(java.awt.Color.white);
            for (Iterator iter = unicodeFont.getEffects().iterator(); iter.hasNext(); ) ((Effect) iter.next()).draw(scratchImage, scratchGraphics, unicodeFont, glyph);
            // The shape will never be needed again.
            glyph.setShape(null);
        }
        width = Math.min(width, texture.getWidth());
        height = Math.min(height, texture.getHeight());
        WritableRaster raster = scratchImage.getRaster();
        int[] row = new int[width];
        for (int y = 0; y < height; y++) {
            raster.getDataElements(0, y, width, 1, row);
            scratchIntBuffer.put(row);
        }
        format = GL12.GL_BGRA;
    }
    // Simple deduplication, doesn't work across pages of course.
    String hash = "";
    try {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(glyphPixels);
        BigInteger bigInt = new BigInteger(1, md.digest());
        hash = bigInt.toString(16);
    } catch (NoSuchAlgorithmException ex) {
    }
    scratchByteBuffer.clear();
    scratchIntBuffer.clear();
    try {
        for (int i = 0, n = hashes.size(); i < n; i++) {
            String other = hashes.get(i);
            if (other.equals(hash)) {
                Glyph dupe = pageGlyphs.get(i);
                glyph.setTexture(dupe.texture, dupe.u, dupe.v, dupe.u2, dupe.v2);
                return false;
            }
        }
    } finally {
        hashes.add(hash);
        pageGlyphs.add(glyph);
    }
    Gdx.gl.glTexSubImage2D(texture.glTarget, 0, pageX, pageY, width, height, format, GL11.GL_UNSIGNED_BYTE, glyphPixels);
    float u = pageX / (float) texture.getWidth();
    float v = pageY / (float) texture.getHeight();
    float u2 = (pageX + width) / (float) texture.getWidth();
    float v2 = (pageY + height) / (float) texture.getHeight();
    glyph.setTexture(texture, u, v, u2, v2);
    return true;
}
Also used : BitmapFontData(com.badlogic.gdx.graphics.g2d.BitmapFont.BitmapFontData) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ByteBuffer(java.nio.ByteBuffer) ColorEffect(com.badlogic.gdx.tools.hiero.unicodefont.effects.ColorEffect) WritableRaster(java.awt.image.WritableRaster) ListIterator(java.util.ListIterator) Iterator(java.util.Iterator) BigInteger(java.math.BigInteger) Effect(com.badlogic.gdx.tools.hiero.unicodefont.effects.Effect) ColorEffect(com.badlogic.gdx.tools.hiero.unicodefont.effects.ColorEffect) BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont) MessageDigest(java.security.MessageDigest) Pixmap(com.badlogic.gdx.graphics.Pixmap)

Example 53 with MessageDigest

use of java.security.MessageDigest in project h2o-3 by h2oai.

the class JarHash method cl_init_md5.

private static byte[] cl_init_md5(String jarpath) {
    byte[] ffHash = new byte[16];
    // The default non-MD5
    Arrays.fill(ffHash, (byte) 0xFF);
    if (jarpath == null)
        return ffHash;
    // Ok, pop Jar open & make MD5
    InputStream is = null;
    try {
        is = new FileInputStream(jarpath);
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        byte[] buf = new byte[4096];
        int pos;
        while ((pos = is.read(buf)) > 0) md5.update(buf, 0, pos);
        // haz md5!
        return md5.digest();
    } catch (IOException | NoSuchAlgorithmException e) {
        // No MD5 algo handy???
        Log.err(e);
    } finally {
        try {
            if (is != null)
                is.close();
        } catch (IOException ignore) {
        }
    }
    return ffHash;
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 54 with MessageDigest

use of java.security.MessageDigest in project gradle by gradle.

the class HashUtil method createHash.

public static HashValue createHash(InputStream instr, String algorithm) {
    MessageDigest messageDigest;
    try {
        messageDigest = createMessageDigest(algorithm);
        byte[] buffer = new byte[4096];
        try {
            while (true) {
                int nread = instr.read(buffer);
                if (nread < 0) {
                    break;
                }
                messageDigest.update(buffer, 0, nread);
            }
        } finally {
            instr.close();
        }
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
    return new HashValue(messageDigest.digest());
}
Also used : UncheckedIOException(org.gradle.api.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(org.gradle.api.UncheckedIOException) MessageDigest(java.security.MessageDigest)

Example 55 with MessageDigest

use of java.security.MessageDigest in project hazelcast by hazelcast.

the class MD5Util method toMD5String.

/**
     * Converts given string to MD5 hash
     *
     * @param str str to be hashed with MD5
     */
@SuppressWarnings("checkstyle:magicnumber")
public static String toMD5String(String str) {
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        if (md == null || str == null) {
            return "NULL";
        }
        byte[] byteData = md.digest(str.getBytes(Charset.forName("UTF-8")));
        StringBuilder sb = new StringBuilder();
        for (byte aByteData : byteData) {
            sb.append(Integer.toString((aByteData & 0xff) + 0x100, 16).substring(1));
        }
        return sb.toString();
    } catch (NoSuchAlgorithmException ignored) {
        EmptyStatement.ignore(ignored);
        return null;
    }
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Aggregations

MessageDigest (java.security.MessageDigest)1237 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)613 IOException (java.io.IOException)176 UnsupportedEncodingException (java.io.UnsupportedEncodingException)102 BigInteger (java.math.BigInteger)101 InputStream (java.io.InputStream)72 FileInputStream (java.io.FileInputStream)70 File (java.io.File)62 DigestInputStream (java.security.DigestInputStream)61 Test (org.junit.Test)61 ByteArrayOutputStream (java.io.ByteArrayOutputStream)51 DigestOutputStream (java.security.DigestOutputStream)45 ArrayList (java.util.ArrayList)37 ByteArrayInputStream (java.io.ByteArrayInputStream)31 X509Certificate (java.security.cert.X509Certificate)29 OutputStream (java.io.OutputStream)28 GeneralSecurityException (java.security.GeneralSecurityException)25 Cipher (javax.crypto.Cipher)25 SecretKeySpec (javax.crypto.spec.SecretKeySpec)25 Provider (java.security.Provider)22