UNPKG

gnablib

Version:

A lean, zero dependency library to provide a useful base for your project.

2 lines 1.03 kB
/*! Copyright 2023-2025 the gnablib contributors MPL-1.1 */ import{utf8 as t}from"../../codec/Utf8.js";import{Sha1 as e,Sha256 as r,Sha512 as n}from"../hash/index.js";import{Hmac as o}from"../mac/index.js";import{U32 as i}from"../../primitive/number/U32Static.js";import{sNum as s}from"../../safe/safe.js";export function pbkdf2(e,r,n,a,f){s("keySize",f).natural().atMost(4294967295).throwNot(),s("count",a).natural().throwNot();const m=r instanceof Uint8Array?r:t.toBytes(r),c=n instanceof Uint8Array?n:t.toBytes(n),p=Math.ceil(f/e.size),u=new Uint8Array(p*e.size),h=new o(e,m);let d=0;for(let t=1;t<=p;t++){h.write(c),i.intoBytesBE(t,h);const r=h.sumIn();h.reset();let n=r;for(let t=1;t<a;t++){h.write(n),n=h.sumIn(),h.reset();for(let t=0;t<n.length;t++)r[t]^=n[t]}u.set(r,d),d+=e.size}return u.subarray(0,f)}export function pbkdf2_hmac_sha1(t,r,n,o){return pbkdf2(new e,t,r,n,o)}export function pbkdf2_hmac_sha256(t,e,n,o){return pbkdf2(new r,t,e,n,o)}export function pbkdf2_hmac_sha512(t,e,r,o){return pbkdf2(new n,t,e,r,o)}