Created
April 17, 2011 10:52
-
-
Save batmat/923937 to your computer and use it in GitHub Desktop.
Sample code showing how to use SHA-512 hash (Well, it's fully standard).
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
package net.batmat.testsha512; | |
import static junit.framework.Assert.assertEquals; | |
import java.math.BigInteger; | |
import java.security.MessageDigest; | |
import java.security.NoSuchAlgorithmException; | |
import java.util.HashMap; | |
import java.util.Map; | |
import org.junit.Test; | |
public class SHA512Test | |
{ | |
@Test | |
public void testHash512() throws NoSuchAlgorithmException | |
{ | |
// petite liste générée avec la commande echo -n "texte" | sha512sum sous Linux | |
Map<String, String> exemples = new HashMap<String, String>(); | |
exemples | |
.put( | |
"le mot de passe", | |
"b2dacb1654eb1fb49f605f5fb97d06d973ac1a499934e3abdec113f8aecc1fd4280f56cfb679cc0ae4d9153f0b8eb68ac683cdaca3a8bf6b4c436bec4ced7cde"); | |
exemples | |
.put( | |
"bonjour", | |
"3041edbcdd46190c0acc504ed195f8a90129efcab173a7b9ac4646b92d04cc80005acaa3554f4b1df839eacadc2491cb623bf3aa6f9eb44f6ea8ca005821d25d"); | |
MessageDigest md = MessageDigest.getInstance("SHA-512"); | |
for (Map.Entry<String, String> entry : exemples.entrySet()) | |
{ | |
byte[] digest = md.digest(entry.getKey().getBytes()); | |
assertEquals(entry.getValue(), String.format("%0128x", new BigInteger(1, digest))); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment