Hash SHA256 like in Python

urlsafe_b64encode(sha256("hello".encode()).digest())
> b'LPJNul-wow4m6DsqxbninhsWHlwfp0JecwQzYpOLmCQ='

How do I run ha256(“hello”.encode()).digest() in OB2?

use hash string block

BLOCK:HashString
  input = "hello"
  hashFunction = SHA256
  => VAR @hashStringOutput
ENDBLOCK

The hashes do not map, I have tried:

BLOCK:HashString
  input = "hello"
  hashFunction = SHA256
  => VAR @hashStringOutput
ENDBLOCK

BLOCK:HexStringToByteArray
  hexString = @hashStringOutput
  => VAR @hexStringToByteArrayOutput
ENDBLOCK

BLOCK:ByteArrayToBase64String
  bytes = @hashStringOutput
  => VAR @byteArrayToBase64StringOutput
ENDBLOCK

and got:

>> Hash String (HashString) <<

Computed hash: 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824

Assigned value to variable 'hashStringOutput'

 

>> Hex => Bytes (Hex => Bytes) <<

Converting 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 to a byte array

Assigned value to variable 'hexStringToByteArrayOutput'

 

>> Bytes => Base64 (Bytes => Base64) <<

Converted the byte array to MmNmMjRkYmE1ZmIwYTMwZTI2ZTgzYjJhYzViOWUyOWUxYjE2MWU1YzFmYTc0MjVlNzMwNDMzNjI5MzhiOTgyNA==

try that

BLOCK:ConstantString
  value = "hello"
  => VAR @str
ENDBLOCK

BLOCK:Script
INTERPRETER:NodeJS
INPUT str
BEGIN SCRIPT
const crypto = require('crypto');

const string = str;
const hash = crypto.createHash('sha256');
hash.update(string);
const hashResult = hash.digest();
const base64Result = hashResult.toString('base64');

console.log(base64Result);
END SCRIPT
OUTPUT String @base64Result
ENDBLOCK
1 Like

Thank you this works great. I guess the “digest” part is missing in LoliCode.

1 Like

Have you tried using HMAC instead?