0

I want to encrypt a String using gnu.crypto.hash.Whirlpool hashing.

The encrypt should encrypt the password and should return the encrypted pwd. encrypt(pwd);

This method should have implementation for encrypting pwd using gnu jars and whirlpool
hashing algorithm which should be equal to the pwd generated by the below site http://hash.online-convert.com/whirlpool-generator

I tried with the code below but I am unable to get the 512 byets code similar to the whirlpool site generated:

import gnu.crypto.hash.HashFactory;
import gnu.crypto.hash.IMessageDigest;

  public class EncryptPwdWithAPI{
public static void main(String arg[])
{
         encrypt("somepwd");
    }
public static String encrypt(String password)
{
IMessageDigest md = HashFactory.getInstance("WHIRLPOOL");
md.update(input, 0, input.length); 
byte[] digest = md.digest(); 
System.out.println( "Input : "+new String(input)+ "\nPWD : "+new String(digest)
}

}

Azhar
  • 933
  • 1
  • 12
  • 28
  • Please don't include a 'sig.' in posts, never include an email address, and show some effort of your own - SO is not a help-desk. – Andrew Thompson Aug 12 '12 at 10:10
  • :) I tried with the below but I am unable to get the 512 byets code similar to the whirlpool site generated. IMessageDigest md = HashFactory.getInstance("WHIRLPOOL"); md.update(input, 0, input.length); byte[] digest = md.digest(); System.out.println( "Input : "+new String(input)+ "\nPWD : "+new String(digest) ); – Azhar Aug 12 '12 at 10:26
  • When I use the bouncycastle library's implementation of Whirlpool I get the same output as PHP's. – President James K. Polk Jun 29 '14 at 23:07

3 Answers3

3

Ya correct but i was expecting whirlpool hascode with 512 bytes which is equal to the code generated by the online hashcode generator. I got the intended output with JacksumAPI.

Here's some code:

import java.security.NoSuchAlgorithmException;

import jonelo.jacksum.JacksumAPI;
import jonelo.jacksum.algorithm.AbstractChecksum;

public class JacksumTest {
    public static void main(String arg[])
    {
        String password = "somepwd";
        AbstractChecksum checksum = null; 
        try { 
           checksum = JacksumAPI.getChecksumInstance("whirlpool"); 
           checksum.update(password.getBytes());
           System.out.println(checksum.getFormattedValue());
        } catch (NoSuchAlgorithmException nsae) { }
    }
}
Rostyslav Dzinko
  • 39,424
  • 5
  • 49
  • 62
Azhar
  • 933
  • 1
  • 12
  • 28
0

You are directly using the byte values as character values instead you should convert the byte array to hex or base64 encoding to compare it to the values on the whirlpool site. Apache Commons Codec provides hex and base64 functions.

import org.apache.commons.codec.binary.Base64;
...
  Base64.encodeBase64String(digest);
Eelke
  • 20,897
  • 4
  • 50
  • 76
  • Thank you for the reply but it is not returning the result same as generated by [link] http://hash.online-convert.com/whirlpool-generator – Azhar Aug 12 '12 at 20:52
0

@Eelke is correct, but This is how you do it, encoded password should be in the result variable.

    IMessageDigest oldencoder = HashFactory.getInstance(Registry.WHIRLPOOL_HASH);

    byte[] input = password.getBytes();

    oldencoder.update(input, 0, input.length);

    byte[] digest = oldencoder.digest();

    result = gnu.crypto.util.Util.toString(digest).toLowerCase();
Zeronex
  • 434
  • 1
  • 3
  • 8