gnablib
Version:
A lean, zero dependency library to provide a useful base for your project.
2 lines • 623 B
JavaScript
/*! Copyright 2023-2024 the gnablib contributors MPL-1.1 */
import{ContentError as t}from"../../error/ContentError.js";import{LengthError as r}from"../../error/LengthError.js";export class Pkcs7{static padSize(t,r){return t<=r?r:0}static pad(t,e,o=0){const n=new Uint8Array(e),s=e-(t.length-o);if(s<0)throw r.atMost(e,"input.length",e-s);return n.fill(s,e-s),n.set(t.subarray(o)),n}static unpad(r,e=0){0==e&&(e=r.length-1);const o=r[e];if(e-o<-1)throw new t("count too large","last byte",o);for(let n=o-1;n>0;n--)if(r[--e]!==o)throw new t(`expecting ${o}`,`byte:${e}`,r[e]);return r.subarray(0,e)}}export const Pkcs5=Pkcs7;