Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
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;
}
}
}
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:
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.