Search in sources :

Example 96 with MessageDigest

use of java.security.MessageDigest in project NabAlive by jcheype.

the class User method checkPassword.

public void checkPassword(String passwordToCheck) throws NoSuchAlgorithmException {
    MessageDigest md = MessageDigest.getInstance("SHA-1");
    byte[] digest = md.digest((email + passwordToCheck).getBytes(Charset.forName("UTF-8")));
    if (!Arrays.equals(password, digest))
        throw new IllegalArgumentException("bad password");
}
Also used : MessageDigest(java.security.MessageDigest)

Example 97 with MessageDigest

use of java.security.MessageDigest in project Klyph by jonathangerbaud.

the class Utility method hashWithAlgorithm.

private static String hashWithAlgorithm(String algorithm, String key) {
    MessageDigest hash = null;
    try {
        hash = MessageDigest.getInstance(algorithm);
    } catch (NoSuchAlgorithmException e) {
        return null;
    }
    hash.update(key.getBytes());
    byte[] digest = hash.digest();
    StringBuilder builder = new StringBuilder();
    for (int b : digest) {
        builder.append(Integer.toHexString((b >> 4) & 0xf));
        builder.append(Integer.toHexString((b >> 0) & 0xf));
    }
    return builder.toString();
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 98 with MessageDigest

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

the class ProxyDigestAuthenticator method md5.

/**
     * Colon separated value MD5 hash.
     *
     * @param tokens one or more strings
     * @return M5 hash string
     * @throws ProxyAuthenticationException if MD5 algorithm cannot be instantiated
     */
private static String md5(String... tokens) throws ProxyAuthenticationException {
    StringBuilder sb = new StringBuilder(100);
    for (String token : tokens) {
        if (sb.length() > 0) {
            sb.append(':');
        }
        sb.append(token);
    }
    MessageDigest md;
    try {
        md = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException ex) {
        throw new ProxyAuthenticationException(ex.getMessage());
    }
    md.update(sb.toString().getBytes(CHARACTER_SET), 0, sb.length());
    byte[] md5hash = md.digest();
    return bytesToHex(md5hash);
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 99 with MessageDigest

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

the class CheckedFunction6Test method shouldRecoverNonNull.

@Test
public void shouldRecoverNonNull() {
    Function6<String, String, String, String, String, String, MessageDigest> recover = digest.recover(throwable -> null);
    MessageDigest md5 = recover.apply("M", "D", "5", "", "", "");
    assertThat(md5).isNotNull();
    assertThat(md5.getAlgorithm()).isEqualToIgnoringCase("MD5");
    assertThat(md5.getDigestLength()).isEqualTo(16);
    Try<MessageDigest> unknown = Function6.liftTry(recover).apply("U", "n", "k", "n", "o", "wn");
    assertThat(unknown).isNotNull();
    assertThat(unknown.isFailure()).isTrue();
    assertThat(unknown.getCause()).isNotNull().isInstanceOf(NullPointerException.class);
    assertThat(unknown.getCause().getMessage()).isNotEmpty().isEqualToIgnoringCase("recover return null for class java.security.NoSuchAlgorithmException: Unknown MessageDigest not available");
}
Also used : MessageDigest(java.security.MessageDigest) Test(org.junit.Test)

Example 100 with MessageDigest

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

the class CheckedFunction7Test method shouldUncheckedWork.

@Test
public void shouldUncheckedWork() {
    Function7<String, String, String, String, String, String, String, MessageDigest> unchecked = digest.unchecked();
    MessageDigest md5 = unchecked.apply("M", "D", "5", "", "", "", "");
    assertThat(md5).isNotNull();
    assertThat(md5.getAlgorithm()).isEqualToIgnoringCase("MD5");
    assertThat(md5.getDigestLength()).isEqualTo(16);
}
Also used : MessageDigest(java.security.MessageDigest) Test(org.junit.Test)

Aggregations

MessageDigest (java.security.MessageDigest)1122 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)562 IOException (java.io.IOException)161 UnsupportedEncodingException (java.io.UnsupportedEncodingException)93 BigInteger (java.math.BigInteger)93 FileInputStream (java.io.FileInputStream)68 InputStream (java.io.InputStream)66 File (java.io.File)58 DigestInputStream (java.security.DigestInputStream)56 Test (org.junit.Test)55 ByteArrayOutputStream (java.io.ByteArrayOutputStream)46 DigestOutputStream (java.security.DigestOutputStream)43 ArrayList (java.util.ArrayList)32 ByteArrayInputStream (java.io.ByteArrayInputStream)29 OutputStream (java.io.OutputStream)27 X509Certificate (java.security.cert.X509Certificate)27 GeneralSecurityException (java.security.GeneralSecurityException)25 Cipher (javax.crypto.Cipher)23 Provider (java.security.Provider)22 SecretKeySpec (javax.crypto.spec.SecretKeySpec)22