- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathJavaSHA_256.java
More file actions
Latest commit
26 lines (22 loc) · 853 Bytes
/
Copy pathJavaSHA_256.java
File metadata and controls
26 lines (22 loc) · 853 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// https://www.hackerrank.com/challenges/sha-256/problem
importjava.security.MessageDigest;
importjava.security.NoSuchAlgorithmException;
importjava.util.Scanner;
publicclassJavaSHA_256 {
publicstaticvoidmain(String[] args) throwsNoSuchAlgorithmException {
MessageDigestsha256 = MessageDigest.getInstance("sha-256");
Scannerscanner = newScanner(System.in);
Stringmessage = scanner.nextLine();
scanner.close();
byte[] hash = sha256.digest(message.getBytes());
StringhashHexCode = toHex(hash);
System.out.println(hashHexCode);
}
privatestaticStringtoHex(byte[] array) {
StringBuildersb = newStringBuilder(2 * array.length);
for(byteb : array) {
sb.append(String.format("%02x", b&0xff));
}
returnsb.toString();
}
}