mirror of
https://github.com/honojs/hono.git
synced 2024-12-01 11:51:01 +01:00
41 lines
1.0 KiB
TypeScript
41 lines
1.0 KiB
TypeScript
type Algorithm = {
|
|
name: string
|
|
alias: string
|
|
}
|
|
|
|
type Data = string | object | boolean
|
|
|
|
export const sha256 = async (data: Data) => {
|
|
const algorithm: Algorithm = { name: 'SHA-256', alias: 'sha256' }
|
|
const hash = await createHash(data, algorithm)
|
|
return hash
|
|
}
|
|
|
|
export const sha1 = async (data: Data) => {
|
|
const algorithm: Algorithm = { name: 'SHA-1', alias: 'sha1' }
|
|
const hash = await createHash(data, algorithm)
|
|
return hash
|
|
}
|
|
|
|
export const md5 = async (data: Data) => {
|
|
const algorithm: Algorithm = { name: 'MD5', alias: 'md5' }
|
|
const hash = await createHash(data, algorithm)
|
|
return hash
|
|
}
|
|
|
|
export const createHash = async (data: Data, algorithm: Algorithm): Promise<string | null> => {
|
|
if (crypto && crypto.subtle) {
|
|
const buffer = await crypto.subtle.digest(
|
|
{
|
|
name: algorithm.name,
|
|
},
|
|
new TextEncoder().encode(String(data))
|
|
)
|
|
const hash = Array.prototype.map
|
|
.call(new Uint8Array(buffer), (x) => ('00' + x.toString(16)).slice(-2))
|
|
.join('')
|
|
return hash
|
|
}
|
|
return null
|
|
}
|