How to work with byte arrays

You might have noticed a big difference from OB1 to OB2 in the fact that now there’s an actual ByteArray type, and you don’t have to pass base64 strings around in order to work with bytes.

This happens for example with the Hash and Hmac blocks. These blocks take a ByteArray as input and give you a ByteArray as output.

You can feed a String directly to the input (in variable mode) and it will automatically be converted from a UTF-8 string to its corresponding bytes. You can then take the byte array output and reconvert it to a hex or base64 string using the corresponding blocks.

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

BLOCK:Hash
  input = @MYSTRING
  => VAR @HASH
ENDBLOCK

BLOCK:ByteArrayToHexString
  bytes = @HASH
  => VAR @HEX
ENDBLOCK

BLOCK:ByteArrayToBase64String
  bytes = @HASH
  => VAR @BASE64
ENDBLOCK

Note: In the output log of the Hash block you will see this

immagine

But this is only a HEX REPRESENTATION of the ByteArray, it’s not a hex string!!!
As you can see from the variable view, the HASH variable is a ByteArray

immagine

If you instead don’t care about this and just want to deal with strings, you can use the Hash String and Hmac String blocks instead.

Ruri

4 Likes