Bytes and Hex Text
Part of the Auth and Security section of Coddy's Node.js journey. Lesson 1 of 24.
Cryptographic functions work with bytes. A byte is a number from 0 to 255. UTF-8 encodes text as bytes, and a Buffer holds those bytes in Node. Hexadecimal, or hex, writes each byte as two characters using digits 0 to 9 and letters a to f. This makes bytes convenient to store as text.
const bytes = Buffer.from('Ok', 'utf8');
bytes.toString('hex'); // '4f6b'
Buffer.from('4f6b', 'hex').toString('utf8'); // 'Ok'The byte for the letter O is 79 in decimal, written 4f in hex.
This conversion is reversible: it changes the representation, not the information. Encoding a password as hex does not protect it. A password hash is checked by recomputing a derived value instead of decoding it. Here the inputs are ordinary text, and the exercise returns their hex representation so you can compare both forms.
Challenge
EasyThis is a small practice project. Its setup is provided; edit only exercise.js.
Return a hex field containing the supplied text encoded as UTF-8 bytes and then hex. POST {"text":"Hi"} to /result returns {"hex":"4869"}. Text Ada returns 416461.
Try it yourself
export function exercise({ text }) {
return { hex: text };
}
All lessons in Auth and Security
1Passwords
Bytes and Hex TextDeriving a Password KeyA Fresh SaltNever Store PasswordsComparing Key BytesChecking a PasswordSignup RulesPractice on your own: Express playground