forked from alexprut/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
27 lines (24 loc) · 798 Bytes
/
Solution.java
File metadata and controls
27 lines (24 loc) · 798 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
27
import java.io.*;
import java.util.*;
import java.security.*;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.next();
try {
System.out.println(Sha.hash256(s));
} catch (Exception e) {}
}
}
class Sha {
public static String hash256(String data) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(data.getBytes());
return bytesToHex(md.digest());
}
public static String bytesToHex(byte[] bytes) {
StringBuffer result = new StringBuffer();
for (byte byt : bytes) result.append(Integer.toString((byt & 0xff) + 0x100, 16).substring(1));
return result.toString();
}
}