Skip to content

Commit

Permalink
functions to generate SHA2, SHA3 hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
pilinux committed Aug 29, 2023
1 parent 7651b1a commit 4308bfb
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions app/crypto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { createHash } from "node:crypto";

// Calculate a SHA256 hash using the algorithm from the SHA-2 family
export function sha256(content) {
return createHash("sha256").update(content).digest("hex");
}

// Calculate a SHA512 hash using the algorithm from the SHA-2 family
export function sha512(content) {
return createHash("sha512").update(content).digest("hex");
}

// Calculate a SHA256 hash using the algorithm from the SHA-3 family
export function sha3_256(content) {
return createHash("sha3-256").update(content).digest("hex");
}

// Calculate a SHA512 hash using the algorithm from the SHA-3 family
export function sha3_512(content) {
return createHash("sha3-512").update(content).digest("hex");
}

// Convert hexadecimal string to base64 encoded string
export function encodeBase64(hexString) {
// convert hexadecimal string to a Buffer
const buffer = Buffer.from(hexString, "hex");

// convert Buffer to base64 encoded string
const base64String = buffer.toString("base64");

return base64String;
}

// Convert base64 encoded string to hexadecimal string
export function decodeBase64(base64String) {
// convert base64 encoded string to a Buffer
const buffer = Buffer.from(base64String, "base64");

// convert Buffer to a hexadecimal string
const originalString = buffer.toString("hex");

return originalString;
}

0 comments on commit 4308bfb

Please sign in to comment.