Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
25
Task/MD5/Java/md5.java
Normal file
25
Task/MD5/Java/md5.java
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
public class Digester {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(hexDigest("Rosetta code", "MD5"));
|
||||
}
|
||||
|
||||
static String hexDigest(String str, String digestName) {
|
||||
try {
|
||||
MessageDigest md = MessageDigest.getInstance(digestName);
|
||||
byte[] digest = md.digest(str.getBytes(StandardCharsets.UTF_8));
|
||||
char[] hex = new char[digest.length * 2];
|
||||
for (int i = 0; i < digest.length; i++) {
|
||||
hex[2 * i] = "0123456789abcdef".charAt((digest[i] & 0xf0) >> 4);
|
||||
hex[2 * i + 1] = "0123456789abcdef".charAt(digest[i] & 0x0f);
|
||||
}
|
||||
return new String(hex);
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue