MCPcopy Create free account
hub / github.com/Moddable-OpenSource/moddable / PKCS1

Class PKCS1

modules/crypt/etc/pkcs1.js:41–113  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

39import BER from "ber";
40
41export default class PKCS1 {
42 static I2OSP(I, l) {
43 var c = ArrayBuffer.fromBigInt(I);
44 if (l && l > c.byteLength) {
45 // prepend 0
46 var d = l - c.byteLength;
47 var t = new Uint8Array(d);
48 for (var i = 0; i < d; i++) // just in case
49 t[i] = 0x00;
50 c = t.buffer.concat(c);
51 }
52 return c;
53 };
54 static sIS2SP(sI, l) {
55 if (!l) {
56 l = 4;
57 var c = new Uint8Array(l);
58 var skip = true;
59 var i = 0;
60 while (--l >= 0) {
61 var x;
62 if ((x = (sI >>> (l*8))) != 0 || !skip) {
63 c[i++] = x & 0xff;
64 skip = false;
65 }
66 }
67 if (i == 0)
68 c[i++] = 0;
69 c = c.slice(0, i);
70 }
71 else {
72 // l must be <= 4
73 var c = new new Uint8Array(l);
74 var i = 0;
75 while (--l >= 0)
76 c[i++] = (sI >>> (l*8)) & 0xff;
77 }
78 return c.buffer;
79 };
80 static OS2IP(OS) {
81 if (OS instanceof Uint8Array) {
82 let offset = OS.byteOffset;
83 OS = OS.buffer.slice(offset, offset + OS.byteLength);
84 }
85 return BigInt.fromArrayBuffer(OS);
86 };
87 static randint(max) {
88 var i = BigInt.fromArrayBuffer(RNG.get(BigInt.bitLength(max) >>> 3));
89 while (i >= max)
90 i >>>= 1;
91 return i;
92 };
93 static parse(buf, privFlag) {
94 // currently RSA only
95 var key = {};
96 var ber = new BER(buf);
97 if (ber.getTag() != 0x30) // SEQUENCE
98 throw new Error("PKCS1: not a sequence");

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected