Skip to main content
Announcements
Join us at Qlik Connect for 3 magical days of learning, networking,and inspiration! REGISTER TODAY and save!
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

Hash a string : string to sha-256

Hello, 

to hash a string I found attached code. it's delivering a result but non significatives 0 (zeros on the left of the string) are not provided in the result ... 

 

for exemple I get         b1f3daebef8a4e237e7830579a8505378ce360106b69f55805f0b255e82637

but I was expecting 00b1f3daebef8a4e237e7830579a8505378ce360106b69f55805f0b255e82637

 

I can see that the code is manipulating a big integer ... that's probably the reason why zeros are eliminated ...

 

package routines;
import java.security.MessageDigest;
import java.math.BigInteger;
public class SHA {
public static String getMD5HashedPassword(String password, String salt) {
    String sTH;
    if(password == null) {
    if(salt == null) sTH = "";
    else sTH = salt;
    }
    else {
    if(salt == null) sTH = password;
    else sTH = password + salt;
    }
   
    try {
    MessageDigest md5 = MessageDigest.getInstance("SHA-256");
    md5.update(sTH.getBytes());
    BigInteger hash = new BigInteger(1, md5.digest());
    return hash.toString(16);
    } 
    catch (Exception e) {
    return null;
    }
   }
}

Labels (3)
4 Replies
Anonymous
Not applicable
Author

This code can hardly work. I guess the original version was intent to build MD5 hashes and these hashes are typically numbers.

The SHA-256 hash would be by far to large for a number therefore such hashes are typically Strings (like a guid).

    public static String buildSHA256Hash(String content) {
    	
    	if (content == null) {
    		return null;
    	}
    	// calculate hash
    	MessageDigest messageDigest;
		try {
			messageDigest = MessageDigest.getInstance("SHA-256");
		} catch (NoSuchAlgorithmException e) {
			throw new RuntimeException("Digest Algorithm SHA-256 could not be found in this environment.", e);
		}
    	
		final byte[] result = messageDigest.digest(content.getBytes(Charset.forName("UTF-8")));
		
		// convert hash to requested encoding
		return Base64.encodeToBase64String(result);
    }

And here the Base64 class. This is a Talend Routine you should create:



To see the whole post, download it here
OriginalPost.pdf
manodwhb
Champion II
Champion II

Instead of bigint you need to convert into string. Or after the value you got you convert into sting add leading zeros.
Anonymous
Not applicable
Author

Bonjour jlolling,

Sorry, but I am a beginner and do not know how to "use" the code you posted.

With the one I posted, I was calling it from a tmap to convert row3.In just giving it as an input and the hashkey in addition :

SHA.getMD5HashedString(row3.In , context.HashKey)

If I create the talend routine you recommended to me, how can I use it in a simple manner ?

regards
Damien
nar
Contributor
Contributor

Hi @DamienBlondel ,

Did you find the solution to this, I'm getting same issue with leading zeros, If you find the solution could you please help me on this.