| 1 | |
package jp.co.y_net.amm.common; |
| 2 | |
|
| 3 | |
import java.security.MessageDigest; |
| 4 | |
import java.security.NoSuchAlgorithmException; |
| 5 | |
|
| 6 | 0 | public class CalcHash { |
| 7 | 0 | |
| 8 | |
public static String passowrdHash(String password, String account) { |
| 9 | 0 | |
| 10 | 0 | |
| 11 | |
|
| 12 | 0 | |
| 13 | 0 | |
| 14 | |
|
| 15 | |
|
| 16 | 0 | |
| 17 | 0 | return getSha256(account+":"+password); |
| 18 | |
} |
| 19 | 0 | public static String challengeResponse(String phash, String chvalue) { |
| 20 | |
|
| 21 | 0 | return getSha256(phash+":"+chvalue); |
| 22 | |
} |
| 23 | |
|
| 24 | |
|
| 25 | 0 | |
| 26 | 0 | private static String getSha256(String target) { |
| 27 | 0 | MessageDigest md = null; |
| 28 | 0 | StringBuilder buf = new StringBuilder(); |
| 29 | 0 | try { |
| 30 | 0 | md = MessageDigest.getInstance("SHA-256"); |
| 31 | 0 | md.update(target.getBytes()); |
| 32 | 0 | byte[] digest = md.digest(); |
| 33 | 0 | |
| 34 | 0 | for (int i = 0; i < digest.length; i++) { |
| 35 | 0 | buf.append(String.format("%02x", digest[i])); |
| 36 | 0 | } |
| 37 | 0 | |
| 38 | 0 | } catch (NoSuchAlgorithmException e) { |
| 39 | 0 | throw new RuntimeException(e); |
| 40 | 0 | } |
| 41 | |
|
| 42 | 0 | return buf.toString(); |
| 43 | |
} |
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | 0 | |
| 48 | 0 | public static void main(String[] args) { |
| 49 | 0 | System.out.println(passowrdHash("p@ssw0rd", "ryoma.sakamoto@example.com")); |
| 50 | 0 | System.out.println(passowrdHash("test", "kondou@example.com")); |
| 51 | 0 | System.out.println(passowrdHash("testtest", "okita@example.com")); |
| 52 | 0 | System.out.println(passowrdHash("testtest", "admin")); |
| 53 | 0 | } |
| 54 | |
|
| 55 | |
} |