Skip to content

Commit

Permalink
Updating demo
Browse files Browse the repository at this point in the history
  • Loading branch information
eoftedal committed Dec 13, 2011
1 parent 1c3b403 commit da39bf9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
8 changes: 8 additions & 0 deletions README
@@ -0,0 +1,8 @@
Allows you to test hashextension




You need bouncycastle to use this
I used bcprov-jdk16-146.jar

29 changes: 15 additions & 14 deletions src/HashExtender.java → src/ExampleHashExtender.java
@@ -1,48 +1,49 @@
import java.security.Security;

import org.bouncycastle.crypto.digests.GeneralDigest;
import org.bouncycastle.crypto.digests.MD5Digest;
import org.bouncycastle.crypto.digests.SHA1Digest;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Hex;

public class HashExtender {
public class ExampleHashExtender {
public static void main(String[] args) {
try {
Security.addProvider(new BouncyCastleProvider());

byte[] input = { 'h', 'e', 'l', 'l', 'o' };
byte[] input2 = { 'w', 'o', 'r', 'l', 'd' };
byte[] input = "Hello".getBytes();
byte[] input2 = "World".getBytes();
if (args.length == 2) {
input = args[0].getBytes();
input2 = args[1].getBytes();
}

//Modify here if you want to use MD5, SHA1 or SHA256
GeneralDigest digester = new SHA1Digest();

//Do a hash of the input
digester.update(input, 0, input.length);
byte[] digest = new byte[digester.getDigestSize()];
digester.doFinal(digest, 0);
System.out.println("Original hash: " + new String(Hex.encode(digest)));

System.out.println(new String(Hex.encode(digest)));

//Create an extended hash
DigestExtender extender = new DigestExtender();
byte[] newDigest = extender.extend(digester, digest, input2);
System.out.println(new String(Hex.encode(newDigest)));
System.out.println("Extended hash: " + new String(Hex.encode(newDigest)));

//Calculate the padding and build a hash from scratch
byte[] pad = extender.getPad(digester,input.length);


byte[] padded = join(input, pad);
digester.reset();
digester.update(padded, 0, padded.length);
//extender.printParams(digester);

System.out.println("Padding : " + new String(Hex.encode(pad)));
byte[] full = join(padded, input2);
System.out.println(new String(Hex.encode(full)));
System.out.println("Full input : " + new String(Hex.encode(full)));
digester.reset();
digester.update(full, 0, full.length);
digester.doFinal(digest, 0);
System.out.println(new String(Hex.encode(digest)));

System.out.println("Full hash : " + new String(Hex.encode(digest)));
System.out.println("If everything went ok, the full hash should be equal to the extended hash");

} catch (Exception ex) {
ex.printStackTrace();
Expand Down

0 comments on commit da39bf9

Please sign in to comment.