1

i am trying to convert a java api to cd1 api of encryption... i have in java this method:

private static byte[] encodePassword(String password,String salt) throws UnsupportedEncodingException 
         {                 
    String mergedPasswordAndSalt =mergePasswordAndSalt(password, salt);
    SHA512Digest digester =new  SHA512Digest();        
    byte[] hash = new byte[digester.getDigestSize()];   
             System.out.println("init hash= "+Base64.encode(hash));
    try {
        hash = mergedPasswordAndSalt.getBytes("UTF-8");  
        System.out.println("init merged= "+Base64.encode(hash));
        digester.doFinal(hash,0);  
          System.out.println("after");
    } catch (Exception ex) {
        System.out.println("Exception");                 
    }          
    for (int  i = 1; i < ITERATIONS; ++i) {            
        digester.doFinal(Bytes.concat(hash, mergedPasswordAndSalt.getBytes("UTF-8")),0); 
    }                 
    return hash;
}

and i am trying to use SHA512Digest in the same way that MessageDigest (in java) do:

private static byte[] encodePassword(String password,String salt) throws UnsupportedEncodingException 
         {                 
    String mergedPasswordAndSalt =mergePasswordAndSalt(password, salt);
    SHA512Digest digester =new  SHA512Digest();

    byte[] hash = null;
    try {
        hash = mergedPasswordAndSalt.getBytes("UTF-8");           
        digester.doFinal(mergedPasswordAndSalt.getBytes("UTF-8"),0);            
    } catch (Exception ex) {
        System.out.println("Exception");                 
    }                    
    for (int i = 1; i < ITERATIONS; ++i) {            
        digester.doFinal(Bytes.concat(hash, mergedPasswordAndSalt.getBytes("UTF-8")),0);           
    }                   
    return hash;
}

The lib is established in my project, but doFinal don't work... i failed to convert java to codename, when i the result of the encryption return the initial hash not modified by doFinal

    init hash= AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAA==
init merged= MDAwMHtUZXdSLm1ldE9yTldrV1JTcWVJaHV4ejAvN2ZjeS5HUGIvS1l5c1BybkFZfQ==
Exception

and the method return Sha512{salt='TewR.metOrNWkWRSqeIhuxz0/7fcy.GPb/KYysPrnAY', hash='z4PhNX7vuL3xVChQ1m2AB9Yg5AULVxXcg/SpIdNs6c5H0NE8XYXysP+DGNKHfuwvYw=='}
Zain Elabidine
  • 349
  • 5
  • 16

1 Answers1

0

we need just to create a method that hash a current input and return that hash to be used in the next iteration:

public static byte[] digestt(byte[] bytes) {
Digest digest = new SHA512Digest();
byte[] resBuf = new byte[digest.getDigestSize()];

digest.update(bytes, 0, bytes.length);
digest.doFinal(resBuf, 0);
return resBuf;
}
Zain Elabidine
  • 349
  • 5
  • 16