fork(13) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.security.*;
  7.  
  8. /* Name of the class has to be "Main" only if the class is public. */
  9. class Ideone
  10. {
  11. public static void main (String[] args) throws java.lang.Exception
  12. {
  13. // 可將 Pattern 改成你自己的,用逗號隔開
  14. String pattern="0,1,2,4,6,7,8";
  15. byte[] hash=patternToHash(pattern);
  16.  
  17. System.out.printf("解鎖資料 SHA-1 Hash : \n\t");
  18. for(int b:hash){
  19. System.out.printf("%02x ", (b & 0xFF ) );
  20. }
  21. System.out.printf("\n串接成字串 : \n\t");
  22. System.out.println(byteArrayToString(hash));
  23. }
  24.  
  25. // 將 Byte array Hex code 串成字串
  26. private static String byteArrayToString(byte[] bytes){
  27. StringBuilder hash=new StringBuilder();
  28. for(int b:bytes){
  29. hash.append( String.format("%02x", (b & 0xFF) ) );
  30. }
  31. return hash.toString();
  32. }
  33.  
  34. // 取得 Pattern 的 SHA-1 Hash
  35. private static byte[] patternToHash(String pattern) {
  36. if (pattern == null || pattern.length()==0) {
  37. return null;
  38. }
  39.  
  40. char[] patternChars = pattern.toCharArray();
  41. byte[] res = new byte[patternChars.length];
  42. for (int i = 0; i < patternChars.length; i++) {
  43. res[i] = (byte)(patternChars[i]-'0');
  44. }
  45. try {
  46. MessageDigest md = MessageDigest.getInstance("SHA-1");
  47. byte[] hash = md.digest(res);
  48. return hash;
  49. } catch (NoSuchAlgorithmException nsa) {
  50. return null;
  51. }
  52. }
  53. }
Success #stdin #stdout 0.09s 380480KB
stdin
Standard input is empty
stdout
解鎖資料 SHA-1 Hash : 
	0b 35 b3 c2 9f 2f c6 eb 17 a8 29 eb 75 d7 2d b4 5e 38 59 7b 
串接成字串 : 
	0b35b3c29f2fc6eb17a829eb75d72db45e38597b