Created
April 23, 2015 16:32
-
-
Save cpw/b369ed80eb6027a7a3c3 to your computer and use it in GitHub Desktop.
Secure string pairs
This file contains hidden or 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.nio.charset.StandardCharsets; | |
import java.security.MessageDigest; | |
import java.security.NoSuchAlgorithmException; | |
import javax.xml.bind.DatatypeConverter; | |
public class StringHider | |
{ | |
/** | |
* Generate pairs by doing: | |
* <p><code> | |
* SECRET=The secret to encode; SALT=$(pwgen 64); echo ${SALT}; echo -n ${SALT}${SECRET} | sha256sum | cut -d' ' -f1 | |
* </code> | |
*/ | |
private static final String[][] myStringPairs = | |
{ | |
{ "etheihee1Pai4uuc4shoh3Uacheil9chu1uemieQuoB0IechahGeilo8meeth2Ze", "7005a45764cca26f1cce77830245f642b47ac55ea5dfd198cbad44f627ec9576" }, | |
}; | |
private static final MessageDigest md; | |
static | |
{ | |
try | |
{ | |
md = MessageDigest.getInstance("SHA-256"); | |
} catch (NoSuchAlgorithmException e) | |
{ | |
throw new RuntimeException("WOT?", e); | |
} | |
} | |
public static boolean matches(String input) | |
{ | |
byte[] inputBytes = input.getBytes(StandardCharsets.UTF_8); | |
for (String[] pair : myStringPairs) | |
{ | |
byte[] sha256 = DatatypeConverter.parseHexBinary(pair[1]); | |
String sha256s = DatatypeConverter.printHexBinary(sha256); | |
md.reset(); | |
md.update(pair[0].getBytes(StandardCharsets.US_ASCII)); | |
md.update(inputBytes); | |
byte[] digest = md.digest(); | |
String string = DatatypeConverter.printHexBinary(digest); | |
boolean result = MessageDigest.isEqual(sha256, digest); | |
System.out.println("Digest " + string.length() + " " + string+ " " + result); | |
System.out.println("SHA " + sha256s.length() + " " + sha256s + " " + result); | |
if (result) return true; | |
} | |
return false; | |
} | |
public static void main(String[] args) | |
{ | |
System.out.println("Matches '" + args[0] + "' " + matches(args[0])); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment