Search in sources :

Example 76 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project spring-security by spring-projects.

the class TokenBasedRememberMeServices method makeTokenSignature.

/**
	 * Calculates the digital signature to be put in the cookie. Default value is MD5
	 * ("username:tokenExpiryTime:password:key")
	 */
protected String makeTokenSignature(long tokenExpiryTime, String username, String password) {
    String data = username + ":" + tokenExpiryTime + ":" + password + ":" + getKey();
    MessageDigest digest;
    try {
        digest = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException("No MD5 algorithm available!");
    }
    return new String(Hex.encode(digest.digest(data.getBytes())));
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 77 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project spring-security-oauth by spring-projects.

the class DefaultClientKeyGenerator method extractKey.

public String extractKey(OAuth2ProtectedResourceDetails resource, Authentication authentication) {
    Map<String, String> values = new LinkedHashMap<String, String>();
    if (authentication != null) {
        values.put(USERNAME, authentication.getName());
    }
    values.put(CLIENT_ID, resource.getClientId());
    if (resource.getScope() != null) {
        values.put(SCOPE, OAuth2Utils.formatParameterList(resource.getScope()));
    }
    MessageDigest digest;
    try {
        digest = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException("MD5 algorithm not available.  Fatal (should be in the JDK).");
    }
    try {
        byte[] bytes = digest.digest(values.toString().getBytes("UTF-8"));
        return String.format("%032x", new BigInteger(1, bytes));
    } catch (UnsupportedEncodingException e) {
        throw new IllegalStateException("UTF-8 encoding not available.  Fatal (should be in the JDK).");
    }
}
Also used : BigInteger(java.math.BigInteger) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest) LinkedHashMap(java.util.LinkedHashMap)

Example 78 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException 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 79 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException 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 80 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException 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)

Aggregations

NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1557 MessageDigest (java.security.MessageDigest)590 IOException (java.io.IOException)374 InvalidKeyException (java.security.InvalidKeyException)266 KeyStoreException (java.security.KeyStoreException)200 CertificateException (java.security.cert.CertificateException)163 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)162 UnsupportedEncodingException (java.io.UnsupportedEncodingException)141 KeyManagementException (java.security.KeyManagementException)130 KeyFactory (java.security.KeyFactory)105 NoSuchProviderException (java.security.NoSuchProviderException)102 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)96 SSLContext (javax.net.ssl.SSLContext)91 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)90 KeyStore (java.security.KeyStore)89 UnrecoverableKeyException (java.security.UnrecoverableKeyException)88 InputStream (java.io.InputStream)82 SecureRandom (java.security.SecureRandom)82 Cipher (javax.crypto.Cipher)79 BadPaddingException (javax.crypto.BadPaddingException)75