Convert
nodejs
October 22, 20221 min read

Convert SHA-512 string to base64 in javascript

As we've seen in earlier posts, you can create an SHA-512 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.

In the code below, we are going to create an SHA-512 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("sha512").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 sha512 hash useful. Happy Coding.

Share this blog
Like what you read?
Subscribe to our Newsletter
Subscribe to our email newsletter and unlock access to members-only content and exclusive updates.
About the Author
Satvik
Satvik
Entrepreneur
Satvik is a passionate developer turned Entrepreneur. He is fascinated by JavaScript, Operating System, Deep Learning, AR/VR. He has published several research papers and applied for patents in the field as well. Satvik is a speaker in conferences, meetups talking about Artificial Intelligence, JavaScript and related subjects. His goal is to solve complex problems that people face with automation. Related projects can be seen at - [Projects](/projects)
View all articles
Previous Article
Next Article
September 22, 20222 min read