As a developer, I constantly find myself working in a world ruled by data. The way we store, transmit, and manipulate this data plays a massive role in the functionality of our applications. Today, we’re going to delve into the world of data encryption, specifically focusing on how to convert a SHA-256 string to base64 in Node.js. It’s a process that might seem intricate initially, but I assure you, once we break it down, it’s as clear as day!
As we’ve seen in earlier posts, you can create an SHA-256 hash of a string and generate a hash in string type.
This implementation is for Node.js and server-side frameworks like Expressjs. In case you are looking for a JavaScript on the frontend (Javascript, React.js, Vue.js etc.,), implementation using cryptoJS module for JavaScript.
Converting SHA-256 to base64 in Node.js
In the code below, we are going to create an SHA-256 hash using the default crypto
module and then we use Buffer.from() method to create a buffer from the hash string and then encode the string in base-64
.
import { createHash } from "crypto";
const yourString = "password";
const hash = createHash("sha256").update(yourString).digest("hex");
const b = Buffer.from(hash).toString("base64");
console.log(b);
The output of the above code will be a base64 string-
NWU4ODQ4OThkYTI4MDQ3MTUxZDBlNTZmOGRjNjI5Mjc3MzYwM2QwZDZhYWJiZGQ2MmExMWVmNzIxZDE1NDJkOA==
I’m glad that you found this article to create a base64 string out of sha256 hash useful. And there you have it! We’ve explored together the journey of converting a SHA-256 string to base64 in Node.js. I hope this exploration has been as enlightening for you as it was for me. It’s such powerful techniques that add to the robustness of our applications and increase the security of our data. Remember, in the realm of programming, no knowledge is trivial. Each bit you know has a unique place and function. So, keep those coding gears turning and continue exploring! Happy Coding.