Skip to content

Instantly share code, notes, and snippets.

@mattrossman
Created June 25, 2024 03:21
Show Gist options
  • Save mattrossman/b299fc7f2774cce0a6d5f6234aba5374 to your computer and use it in GitHub Desktop.
Save mattrossman/b299fc7f2774cce0a6d5f6234aba5374 to your computer and use it in GitHub Desktop.
export async function generateRandomSHA1Hash() {
const data = crypto.getRandomValues(new Uint8Array(16))
const hashBuffer = await crypto.subtle.digest("SHA-1", data)
const hashArray = Array.from(new Uint8Array(hashBuffer))
const hashHex = hashArray
.map((byte) => byte.toString(16).padStart(2, "0"))
.join("")
return hashHex
}
export async function generateSHA1Hash(input: string) {
const encoder = new TextEncoder()
const data = encoder.encode(input)
const hashBuffer = await crypto.subtle.digest("SHA-1", data)
const hashArray = Array.from(new Uint8Array(hashBuffer))
const hashHex = hashArray
.map((byte) => byte.toString(16).padStart(2, "0"))
.join("")
return hashHex
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment