SHA512 to UTF8 to byte encryption

Viewed 348

a question:

A parcel service provider requests that the password is encoded in a specific way:

KEY -> UTF8 Encoding -> SHA512

They KEY should be in byte form, not string

currently I have this in Node.js with CryptoJS:

password = CryptoJS.SHA512(CryptoJS.enc.Utf8.parse(key))

or

password = CryptoJS.SHA512(CryptoJS.enc.Utf8.stringify(key))

Don't know which one is the right one.

I need to convert the key to bytes, how do I do that?

1 Answers

Keys are arbitrary sequences of bytes, and SHA-512 works on arbitrary sequences of bytes. However, UTF-8 can't encode arbitrary sequences of bytes. It can only encode Unicode code points. What you're asking for isn't possible. (I suggest posting precisely what the requirement is. It's possible you're misreading it.)

You need another encoding, such as Base64 or Hex. The output of either of those is compatible with UTF-8 (they both output subsets of UTF-8).

That said, this is a very strange request, since you already have exactly the correct input for SHA-512. Converting it to a string and then converting that string back to (likely different) bytes seems a pointless step, but if you need it, you'll need a byte encoding like Base64 or Hex.

Related