Reverse functions in Node.JS

How do I reverse the following line of code to return the variable signature, where the signature variable is supposed to be a Buffer?

let newSignature = btoa(String.fromCharCode(...new Uint8Array(signature)));
let oldSignature = Buffer() // ?

btoa converts a string to base64, it has a complementary function, atob, which reverses this:

atob(string);

String.prototype.charCodeAt Allows you to get the char code of a character:

Array.prototype.map.call(string, (char) => char.charCodeAt(0));

(Array.prototype.map allows you to apply a function to each element in an array and generate a new one, .call is a special function that allows you to apply a method to other datatypes/classes if they meet the requirements of that function.)

You can use this new array to create a Uint8Array:

new Uint8Array(charCodes);

I’m assuming oldSignature is meant to be what we’re finding, so you want a buffer, which you can get by converting the array using Buffer.from:

Buffer.from(uint8arr);

Put it together and you get something like this:

const oldSignature = Buffer.from(new Uint8Array(Array.prototype.map.call(atob(string), (char) => char.charCodeAt(0))));
2 Likes

YESS IT WORKS TYSM! Here, take a solution.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.