Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Want to see what's currently in public preview? We've pulled it all together in one place. Check it out here
cancel
Showing results for 
Search instead for 
Did you mean: 
hvanderborg
Contributor III
Contributor III

String to sha-512, base64

Hi,

 

When trying to connect to an API I have to convert a string to SHA-512. I found a similar question here: https://community.talend.com/t5/Design-and-Development/resolved-String-to-SHA-256/td-p/84300

Although I am not used to java coding, I did get that routine working. It seems the routine is not entirely applicable because my output string is different from the output string the API manual says I should have:

 

The documentation provides this example string:

MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAOUspRHGNhIMnZ6say4k3swVMfKER8UYbVOREqq7t71VL89bagEFZW4rSjkTcBg7fVZCWX5ijgQVEX4xfV5lapcCAwEAAQ==GET/authefec3dc1-9a99-4825-9e09-3e7772fc205d1456823909

 

The documentation says the output string should be:

RK+xg4V0hfgKs27ORFA2mVkXhYIX14zRH/f5bl6KqZB8zxJNgDpC6yHVIlN0uPuDXsR0cfdU9jAM1GdG7yzUSg==

 

The instruction says: "When extracting the byte array from the string, UTF8 decoding should be used. The SHA512 hash should be Base64 encoded."  Probably I need to adjust the routine to decode/encode correctly based on this instruction, I don't know how to apply the necessary changes.

 

My question: Does anyone get the output string above, if so, how did you succeed?

 

Thank you

 

EDIT: edited the title and first sentence because it said SHA-256 instead of SHA-512

PS: I do see the correct output when testing the string on this page: http://approsto.com/sha-generator/ the correct output is show in field 'SHA512 base64 hash'

Labels (4)
1 Solution

Accepted Solutions
hvanderborg
Contributor III
Contributor III
Author

Update: first I managed to get a hex hash, with this code below. 

 

	public static String getHashCodeFromString(String str) throws NoSuchAlgorithmException {
	    MessageDigest md = MessageDigest.getInstance("SHA-512");
	    md.update(str.getBytes());
	    byte byteData[] = md.digest();

	    //convert the byte to hex format method 1
	    StringBuffer hashCodeBuffer = new StringBuffer();
	    for (int i = 0; i < byteData.length; i++) {
	        hashCodeBuffer.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
	    }
	    return hashCodeBuffer.toString();
	}

What I needed is base64. I changed the code above, to the final code below, which gave me the expected result:

 

package routines;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import sun.misc.BASE64Encoder;
public class MyRoutineStringToSHA512test7 {

	public static String getHashCodeFromString(String str) throws NoSuchAlgorithmException {
	    MessageDigest md = MessageDigest.getInstance("SHA-512");
	    md.update(str.getBytes());
	    byte byteData[] = md.digest();

	    String hashCodeBuffer = (new BASE64Encoder()).encode(byteData);
	    return hashCodeBuffer.toString();
	}
}

View solution in original post

2 Replies
hvanderborg
Contributor III
Contributor III
Author

Update: first I managed to get a hex hash, with this code below. 

 

	public static String getHashCodeFromString(String str) throws NoSuchAlgorithmException {
	    MessageDigest md = MessageDigest.getInstance("SHA-512");
	    md.update(str.getBytes());
	    byte byteData[] = md.digest();

	    //convert the byte to hex format method 1
	    StringBuffer hashCodeBuffer = new StringBuffer();
	    for (int i = 0; i < byteData.length; i++) {
	        hashCodeBuffer.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
	    }
	    return hashCodeBuffer.toString();
	}

What I needed is base64. I changed the code above, to the final code below, which gave me the expected result:

 

package routines;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import sun.misc.BASE64Encoder;
public class MyRoutineStringToSHA512test7 {

	public static String getHashCodeFromString(String str) throws NoSuchAlgorithmException {
	    MessageDigest md = MessageDigest.getInstance("SHA-512");
	    md.update(str.getBytes());
	    byte byteData[] = md.digest();

	    String hashCodeBuffer = (new BASE64Encoder()).encode(byteData);
	    return hashCodeBuffer.toString();
	}
}
Anonymous
Not applicable

Hello,

Thanks for posting that you have resolved it by yourself.

Best regards

Sabrina