Calculate MD5 in Node.js
date
May 6, 2021
slug
calculate-md5-in-node
status
Published
summary
This gonna be short.
tags
Coding
type
Post
This gonna be short.
When I was developing Nobelium, I decided to switch to Gravatar as avatar. It's a service made by Automatic (the one who made WordPress), providing globally unique avatars for each person.
Getting the Image is quick and easy, on this guide, seems like I need to calculate md5 hash of an email, after some digging, I found Node.js has this built in. so here's the code:
import { createHash } from 'crypto'
const str = 'abcde'
const hash = createHash('md5').update(str).digest('hex')
So the rest of the work is very easy:
export async function getStaticProps({ params: { slug } }) {
let posts = await getAllPosts()
posts = posts.filter(post => post.status[0] === 'Published')
const post = posts.find(t => t.slug === slug)
const blockMap = await getPostBlocks(post.id)
const emailHash = createHash('md5').update(BLOG.email).digest('hex')
return {
props: { post, blockMap, emailHash },
revalidate: 1
}
}
and I can take email hash as a prop, then pass to the corresponding components, and render the image using
next/image
.