cancel
Showing results for 
Search instead for 
Did you mean: 

HMAC-SHA256 conversion in SAP PO and then conversion into Base 64

former_member445677
Discoverer
0 Kudos

Dear Experts,

Need help on converting an storage key into - HMAC-SHA 256 and then convert it into Base #64 in SAP PO .

I have postman project - which is doing this in JSON , i need it's equivalent way to do in SAP PO.

Below is the piece of code from Postman Project -

"// Hash it using HMAC-SHA256 and then encode using base64", "const storageKey = pm.variables.get(\"azure_storage_key\");", "const signatureBytes = crypto.HmacSHA256(signatureRaw, crypto.enc.Base64.parse(storageKey));", "const signatureEncoded = signatureBytes.toString(crypto.enc.Base64);",

Thanks

Accepted Solutions (0)

Answers (2)

Answers (2)

anupam_ghosh2
Active Contributor
0 Kudos

Hi kdbhardwaj15,

No need to import any libraries. Please try using the code as is.

Regards

Anupam

anupam_ghosh2
Active Contributor
0 Kudos

Hi Kuldeep,

Please try using following code to get the required result

so for these input key="1234" and data="hello world" I get following output

XOD+lv5JiwIfA5+KvNqMJvfzsP29ZsngUQVo33EUv+w=

Regards

Anupam

static String Base64Encoding (String key,String data) throws Exception
    {
		
    	javax.crypto.Mac sha256_HMAC = javax.crypto.Mac.getInstance("HmacSHA256");
    	javax.crypto.spec.SecretKeySpec secret_key = new javax.crypto.spec.SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256");
    	sha256_HMAC.init(secret_key);
    	byte[] bytes = data.getBytes(java.nio.charset.StandardCharsets.UTF_8); 
    	String base64Encoded = java.util.Base64.getEncoder().encodeToString(sha256_HMAC.doFinal(bytes)); 
    	return base64Encoded;
    	
    }

former_member445677
Discoverer

Thanks Anupam,

What are the libraries i need to import to make this code working?

Thanks