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");
}
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();
}
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);
}
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");
}
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);
}
Aggregations