UNPKG

@utilify/crypto

Version:

These functions are useful for ensuring data security and integrity in web and back-end applications.

2 lines (1 loc) 706 B
"use strict";function e(){return"undefined"!=typeof global&&"undefined"!=typeof process||"object"==typeof Deno&&Object.hasOwn(Deno,"version")||"object"==typeof Bun&&Object.hasOwn(Bun,"version")||"undefined"==typeof window}exports.djb2=function(e){let n=5381;for(let t=0;t<e.length;t++)n=33*n^e.charCodeAt(t);return String(n>>>0)},exports.hash=async function(n,t,r="buffer"){if(e())return;const o="string"==typeof n?(new TextEncoder).encode(n):n,i=await crypto.subtle.digest(t,o);return"base64"===r?btoa(String.fromCharCode(...new Uint8Array(i))):"hex"===r?Array.from(new Uint8Array(i)).map((e=>e.toString(16).padStart(2,"0"))).join(""):i},exports.randomUUID=function(){if(!e())return crypto.randomUUID()};