Created
March 2, 2017 17:12
-
-
Save tkizm1/1a0ee0917a891bd0b9a5cb97e3eb4bc5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.security.InvalidKeyException; | |
import java.security.NoSuchAlgorithmException; | |
import java.security.SignatureException; | |
import java.util.Formatter; | |
import java.io.BufferedReader; | |
import java.nio.charset.Charset; | |
import java.io.FileReader; | |
import java.math.BigInteger; | |
import javax.crypto.Mac; | |
import javax.crypto.spec.SecretKeySpec; | |
public class t { | |
private static String toHexString(byte[] bytes) { | |
Formatter formatter = new Formatter(); | |
for (byte b : bytes) { | |
formatter.format("%02x", b); | |
} | |
return formatter.toString(); | |
} | |
public static String encode(String data, String key) throws Exception { | |
final Charset asciiCs = Charset.forName("UTF-8"); | |
final Mac sha256_HMAC = Mac.getInstance("HmacSHA256"); | |
final SecretKeySpec secret_key = new javax.crypto.spec.SecretKeySpec(asciiCs.encode(key).array(), "HmacSHA256"); | |
sha256_HMAC.init(secret_key); | |
final byte[] mac_data = sha256_HMAC.doFinal(asciiCs.encode(data).array()); | |
String result = ""; | |
for (final byte element : mac_data) | |
{ | |
result += Integer.toString((element & 0xff) + 0x100, 16).substring(1); | |
} | |
System.out.println("Result:[" + result + "]"); | |
return result; | |
} | |
public static void main(String[] args) throws Exception { | |
BufferedReader br = new BufferedReader(new FileReader("1.txt")); | |
try { | |
StringBuilder sb = new StringBuilder(); | |
String line = br.readLine(); | |
while (line != null) { | |
sb.append(line); | |
sb.append(System.lineSeparator()); | |
line = br.readLine(); | |
} | |
String everything = sb.toString(); | |
String hmac = encode(everything, "111111"); | |
System.out.println(hmac); | |
} finally { | |
br.close(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment