/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
import java.security.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
{
// 可將 Pattern 改成你自己的,用逗號隔開
String pattern
="0,1,2,4,6,7,8"; byte[] hash=patternToHash(pattern);
System.
out.
printf("解鎖資料 SHA-1 Hash : \n\t"); for(int b:hash){
System.
out.
printf("%02x ",
(b
& 0xFF
) ); }
System.
out.
printf("\n串接成字串 : \n\t"); System.
out.
println(byteArrayToString
(hash
)); }
// 將 Byte array Hex code 串成字串
private static String byteArrayToString
(byte[] bytes
){ StringBuilder hash=new StringBuilder();
for(int b:bytes){
hash.
append( String.
format("%02x",
(b
& 0xFF
) ) ); }
return hash.toString();
}
// 取得 Pattern 的 SHA-1 Hash
private static byte[] patternToHash
(String pattern
) { if (pattern == null || pattern.length()==0) {
return null;
}
char[] patternChars = pattern.toCharArray();
byte[] res = new byte[patternChars.length];
for (int i = 0; i < patternChars.length; i++) {
res[i] = (byte)(patternChars[i]-'0');
}
try {
byte[] hash = md.digest(res);
return hash;
return null;
}
}
}
LyogcGFja2FnZSB3aGF0ZXZlcjsgLy8gZG9uJ3QgcGxhY2UgcGFja2FnZSBuYW1lISAqLwoKaW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLio7CmltcG9ydCBqYXZhLmlvLio7CmltcG9ydCBqYXZhLnNlY3VyaXR5Lio7CgovKiBOYW1lIG9mIHRoZSBjbGFzcyBoYXMgdG8gYmUgIk1haW4iIG9ubHkgaWYgdGhlIGNsYXNzIGlzIHB1YmxpYy4gKi8KY2xhc3MgSWRlb25lCnsKCXB1YmxpYyBzdGF0aWMgdm9pZCBtYWluIChTdHJpbmdbXSBhcmdzKSB0aHJvd3MgamF2YS5sYW5nLkV4Y2VwdGlvbgoJewoJCS8vIOWPr+WwhyBQYXR0ZXJuIOaUueaIkOS9oOiHquW3seeahO+8jOeUqOmAl+iZn+malOmWiwoJCVN0cmluZyBwYXR0ZX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