@eyhn/crypto
Advanced tools
Comparing version 1.0.6 to 1.0.8
@@ -10,5 +10,6 @@ import pkcs1encrypt from "./pkcs1/encrypt"; | ||
import pkcs1decrypt from "./pkcs1/decrypt"; | ||
import pkcs1sign from "./pkcs1/sign"; | ||
import { arrayBufferToBigInt } from "./bigint"; | ||
import sha1 from "./sha1"; | ||
import sha256 from "./sha256"; | ||
import pkcs1verify from "./pkcs1/verify"; | ||
declare const _default: { | ||
@@ -23,4 +24,5 @@ rsa: { | ||
generate: typeof pkcs1generate; | ||
sign: typeof pkcs1sign; | ||
verify: typeof pkcs1verify; | ||
}; | ||
sha1: typeof sha1; | ||
sha256: typeof sha256; | ||
@@ -27,0 +29,0 @@ tools: { |
@@ -7,3 +7,3 @@ (function (factory) { | ||
else if (typeof define === "function" && define.amd) { | ||
define(["require", "exports", "./pkcs1/encrypt", "./utils/hex", "./utils/text", "./pkcs1/padding", "./pkcs1/unpadding", "./pkcs1/dopublic", "./pkcs1/doprivate", "./pkcs1/generate", "./pkcs1/decrypt", "./bigint", "./sha1", "./sha256"], factory); | ||
define(["require", "exports", "./pkcs1/encrypt", "./utils/hex", "./utils/text", "./pkcs1/padding", "./pkcs1/unpadding", "./pkcs1/dopublic", "./pkcs1/doprivate", "./pkcs1/generate", "./pkcs1/decrypt", "./pkcs1/sign", "./bigint", "./sha256", "./pkcs1/verify"], factory); | ||
} | ||
@@ -21,5 +21,6 @@ })(function (require, exports) { | ||
const decrypt_1 = require("./pkcs1/decrypt"); | ||
const sign_1 = require("./pkcs1/sign"); | ||
const bigint_1 = require("./bigint"); | ||
const sha1_1 = require("./sha1"); | ||
const sha256_1 = require("./sha256"); | ||
const verify_1 = require("./pkcs1/verify"); | ||
return { | ||
@@ -33,5 +34,6 @@ rsa: { | ||
doprivate: doprivate_1.default, | ||
generate: generate_1.default | ||
generate: generate_1.default, | ||
sign: sign_1.default, | ||
verify: verify_1.default | ||
}, | ||
sha1: sha1_1.default, | ||
sha256: sha256_1.default, | ||
@@ -38,0 +40,0 @@ tools: { |
@@ -1,1 +0,1 @@ | ||
export default function pkcs1doprivate(d: ArrayBuffer, n: ArrayBuffer, data: ArrayBuffer): Uint8Array; | ||
export default function pkcs1doprivate(d: ArrayBuffer, n: ArrayBuffer, data: ArrayBuffer): ArrayBuffer | SharedArrayBuffer; |
@@ -17,3 +17,3 @@ (function (factory) { | ||
const ni = bigint_1.arrayBufferToBigInt(n); | ||
return new Uint8Array(datai.modPow(di, ni).toArray(256).value); | ||
return new Uint8Array(datai.modPow(di, ni).toArray(256).value).buffer; | ||
} | ||
@@ -20,0 +20,0 @@ exports.default = pkcs1doprivate; |
import * as BigInt from "big-integer"; | ||
export default function pkcs1dopublic(x: ArrayBuffer, n: ArrayBuffer, e: BigInt.BigNumber): Uint8Array; | ||
export default function pkcs1dopublic(x: ArrayBuffer, n: ArrayBuffer, e: BigInt.BigNumber): ArrayBuffer | SharedArrayBuffer; |
@@ -16,3 +16,3 @@ (function (factory) { | ||
const ni = bigint_1.arrayBufferToBigInt(n); | ||
return new Uint8Array(xi.modPow(e, ni).toArray(256).value); | ||
return new Uint8Array(xi.modPow(e, ni).toArray(256).value).buffer; | ||
} | ||
@@ -19,0 +19,0 @@ exports.default = pkcs1dopublic; |
import * as BigInt from "big-integer"; | ||
export default function pkcs1encrypt(buffer: ArrayBuffer, n: ArrayBuffer, e: BigInt.BigNumber): Uint8Array; | ||
export default function pkcs1encrypt(buffer: ArrayBuffer, n: ArrayBuffer, e: BigInt.BigNumber): ArrayBuffer | SharedArrayBuffer; |
@@ -6,9 +6,9 @@ import * as BigInt from "big-integer"; | ||
export default function pkcs1generate(bitlength: number, expt: number, seed?: string): { | ||
n: Uint8Array; | ||
d: Uint8Array; | ||
p: Uint8Array; | ||
q: Uint8Array; | ||
dmp1: Uint8Array; | ||
dmq1: Uint8Array; | ||
coeff: Uint8Array; | ||
n: ArrayBuffer | SharedArrayBuffer; | ||
d: ArrayBuffer | SharedArrayBuffer; | ||
p: ArrayBuffer | SharedArrayBuffer; | ||
q: ArrayBuffer | SharedArrayBuffer; | ||
dmp1: ArrayBuffer | SharedArrayBuffer; | ||
dmq1: ArrayBuffer | SharedArrayBuffer; | ||
coeff: ArrayBuffer | SharedArrayBuffer; | ||
}; |
@@ -17,3 +17,3 @@ (function (factory) { | ||
function generateRandomBiting(bitlength, prng) { | ||
const x = new Uint8Array(bitlength / 8 + 1), t = bitlength & 7; | ||
const x = new Uint8Array(bitlength >> 3 + 1), t = bitlength & 7; | ||
prng(x); | ||
@@ -42,3 +42,3 @@ if (t > 0) | ||
const rng = seed ? alea_1.default(seed) : prng_1.default(); | ||
const qs = bitlength / 2; | ||
const qs = bitlength >> 1; | ||
const ee = BigInt(expt); | ||
@@ -71,9 +71,9 @@ let p, q; | ||
return { | ||
n: new Uint8Array(n.toArray(256).value), | ||
d: new Uint8Array(d.toArray(256).value), | ||
p: new Uint8Array(p.toArray(256).value), | ||
q: new Uint8Array(q.toArray(256).value), | ||
dmp1: new Uint8Array(dmp1.toArray(256).value), | ||
dmq1: new Uint8Array(dmq1.toArray(256).value), | ||
coeff: new Uint8Array(coeff.toArray(256).value) | ||
n: new Uint8Array(n.toArray(256).value).buffer, | ||
d: new Uint8Array(d.toArray(256).value).buffer, | ||
p: new Uint8Array(p.toArray(256).value).buffer, | ||
q: new Uint8Array(q.toArray(256).value).buffer, | ||
dmp1: new Uint8Array(dmp1.toArray(256).value).buffer, | ||
dmq1: new Uint8Array(dmq1.toArray(256).value).buffer, | ||
coeff: new Uint8Array(coeff.toArray(256).value).buffer | ||
}; | ||
@@ -80,0 +80,0 @@ } |
@@ -1,1 +0,1 @@ | ||
export default function pkcs1padding(s: ArrayBuffer, n: number): Uint8Array; | ||
export default function pkcs1padding(s: ArrayBuffer, n: number): ArrayBuffer | SharedArrayBuffer; |
@@ -32,3 +32,3 @@ (function (factory) { | ||
ba.set(new Uint8Array(s), random.byteLength + 3); | ||
return ba; | ||
return ba.buffer; | ||
} | ||
@@ -35,0 +35,0 @@ exports.default = pkcs1padding; |
@@ -14,5 +14,17 @@ (function (factory) { | ||
if (typeof window !== "undefined") { | ||
return (ba) => window.crypto.getRandomValues(ba); | ||
// Browser | ||
if (typeof window.crypto !== "undefined") { | ||
return (ba) => window.crypto.getRandomValues(ba); | ||
} | ||
else { | ||
return (ba) => { | ||
var l = ba.length; | ||
while (l--) { | ||
ba[l] = Math.floor(Math.random() * 256); | ||
} | ||
}; | ||
} | ||
} | ||
else { | ||
// Node.JS | ||
const crypto = require('crypto'); | ||
@@ -19,0 +31,0 @@ return (ba) => crypto.randomFillSync(ba); |
@@ -12,5 +12,121 @@ (function (factory) { | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const jssha256 = require('js-sha256'); | ||
const K = [0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, | ||
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, | ||
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, | ||
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, | ||
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, | ||
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, | ||
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, | ||
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2]; | ||
const INIT = new Uint32Array([0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19]); | ||
const EXTRA = [-2147483648, 8388608, 32768, 128]; | ||
const SHIFT = [24, 16, 8, 0]; | ||
function block(blocks, hash, frist) { | ||
let h0 = hash[0], h1 = hash[1], h2 = hash[2], h3 = hash[3], h4 = hash[4], h5 = hash[5], h6 = hash[6], h7 = hash[7], j, s0, s1, maj, t1, t2, ch, ab, da, cd, bc; | ||
for (j = 16; j < 64; ++j) { | ||
// rightrotate | ||
t1 = blocks[j - 15]; | ||
s0 = ((t1 >>> 7) | (t1 << 25)) ^ ((t1 >>> 18) | (t1 << 14)) ^ (t1 >>> 3); | ||
t1 = blocks[j - 2]; | ||
s1 = ((t1 >>> 17) | (t1 << 15)) ^ ((t1 >>> 19) | (t1 << 13)) ^ (t1 >>> 10); | ||
blocks[j] = blocks[j - 16] + s0 + blocks[j - 7] + s1 << 0; | ||
} | ||
bc = h1 & h2; | ||
for (j = 0; j < 64; j += 4) { | ||
if (frist) { | ||
ab = 704751109; | ||
t1 = blocks[0] - 210244248; | ||
h7 = t1 - 1521486534 << 0; | ||
h3 = t1 + 143694565 << 0; | ||
frist = false; | ||
} | ||
else { | ||
s0 = ((h0 >>> 2) | (h0 << 30)) ^ ((h0 >>> 13) | (h0 << 19)) ^ ((h0 >>> 22) | (h0 << 10)); | ||
s1 = ((h4 >>> 6) | (h4 << 26)) ^ ((h4 >>> 11) | (h4 << 21)) ^ ((h4 >>> 25) | (h4 << 7)); | ||
ab = h0 & h1; | ||
maj = ab ^ (h0 & h2) ^ bc; | ||
ch = (h4 & h5) ^ (~h4 & h6); | ||
t1 = h7 + s1 + ch + K[j] + blocks[j]; | ||
t2 = s0 + maj; | ||
h7 = h3 + t1 << 0; | ||
h3 = t1 + t2 << 0; | ||
} | ||
s0 = ((h3 >>> 2) | (h3 << 30)) ^ ((h3 >>> 13) | (h3 << 19)) ^ ((h3 >>> 22) | (h3 << 10)); | ||
s1 = ((h7 >>> 6) | (h7 << 26)) ^ ((h7 >>> 11) | (h7 << 21)) ^ ((h7 >>> 25) | (h7 << 7)); | ||
da = h3 & h0; | ||
maj = da ^ (h3 & h1) ^ ab; | ||
ch = (h7 & h4) ^ (~h7 & h5); | ||
t1 = h6 + s1 + ch + K[j + 1] + blocks[j + 1]; | ||
t2 = s0 + maj; | ||
h6 = h2 + t1 << 0; | ||
h2 = t1 + t2 << 0; | ||
s0 = ((h2 >>> 2) | (h2 << 30)) ^ ((h2 >>> 13) | (h2 << 19)) ^ ((h2 >>> 22) | (h2 << 10)); | ||
s1 = ((h6 >>> 6) | (h6 << 26)) ^ ((h6 >>> 11) | (h6 << 21)) ^ ((h6 >>> 25) | (h6 << 7)); | ||
cd = h2 & h3; | ||
maj = cd ^ (h2 & h0) ^ da; | ||
ch = (h6 & h7) ^ (~h6 & h4); | ||
t1 = h5 + s1 + ch + K[j + 2] + blocks[j + 2]; | ||
t2 = s0 + maj; | ||
h5 = h1 + t1 << 0; | ||
h1 = t1 + t2 << 0; | ||
s0 = ((h1 >>> 2) | (h1 << 30)) ^ ((h1 >>> 13) | (h1 << 19)) ^ ((h1 >>> 22) | (h1 << 10)); | ||
s1 = ((h5 >>> 6) | (h5 << 26)) ^ ((h5 >>> 11) | (h5 << 21)) ^ ((h5 >>> 25) | (h5 << 7)); | ||
bc = h1 & h2; | ||
maj = bc ^ (h1 & h3) ^ cd; | ||
ch = (h5 & h6) ^ (~h5 & h7); | ||
t1 = h4 + s1 + ch + K[j + 3] + blocks[j + 3]; | ||
t2 = s0 + maj; | ||
h4 = h0 + t1 << 0; | ||
h0 = t1 + t2 << 0; | ||
} | ||
hash[0] = hash[0] + h0 << 0; | ||
hash[1] = hash[1] + h1 << 0; | ||
hash[2] = hash[2] + h2 << 0; | ||
hash[3] = hash[3] + h3 << 0; | ||
hash[4] = hash[4] + h4 << 0; | ||
hash[5] = hash[5] + h5 << 0; | ||
hash[6] = hash[6] + h6 << 0; | ||
hash[7] = hash[7] + h7 << 0; | ||
} | ||
function sha256(buf) { | ||
return jssha256.arrayBuffer(buf); | ||
const h = new Uint32Array(INIT); | ||
const blocks = new Array(17); | ||
blocks.fill(0, 1, 16); | ||
const bufview = new Uint8Array(buf); | ||
let index = 0, i = 0, hashed = false; | ||
while (index < buf.byteLength) { | ||
blocks.fill(0, 1, 16); | ||
for (i = 0; index < buf.byteLength && i < 64; index++) { | ||
blocks[i >> 2] |= bufview[index] << SHIFT[i++ & 3]; | ||
} | ||
if (i >= 64) { | ||
block(blocks, h, !hashed); | ||
hashed = true; | ||
} | ||
if (index >= buf.byteLength) { | ||
blocks[i >> 2] |= EXTRA[i & 3]; | ||
if (i >= 56) { | ||
if (i < 64) { | ||
block(blocks, h, !hashed); | ||
hashed = true; | ||
} | ||
blocks[0] = this.block; | ||
blocks.fill(0, 1, 16); | ||
} | ||
let bitLength = buf.byteLength * 8, hBitLength = 0; | ||
while (bitLength > 4294967295) { | ||
hBitLength += bitLength / 4294967296 << 0; | ||
bitLength = bitLength % 4294967296; | ||
} | ||
blocks[14] = hBitLength; | ||
blocks[15] = bitLength; | ||
block(blocks, h, !hashed); | ||
hashed = true; | ||
} | ||
} | ||
const dataView = new DataView(new ArrayBuffer(32)); | ||
for (let i = 0; i < 8; i++) { | ||
dataView.setUint32(i * 4, h[i]); | ||
} | ||
return dataView.buffer; | ||
} | ||
@@ -17,0 +133,0 @@ exports.default = sha256; |
{ | ||
"name": "@eyhn/crypto", | ||
"version": "1.0.6", | ||
"version": "1.0.8", | ||
"main": "lib/index.js", | ||
@@ -14,12 +14,11 @@ "types": "lib/index.d.ts", | ||
"devDependencies": { | ||
"@types/jest": "^24.0.11", | ||
"jest": "^24.5.0", | ||
"ts-jest": "^24.0.0", | ||
"typescript": "^3.3.4000" | ||
"@types/node": "^12.11.7", | ||
"@types/jest": "^24.0.20", | ||
"jest": "^24.9.0", | ||
"ts-jest": "^24.1.0", | ||
"typescript": "^3.6.4" | ||
}, | ||
"dependencies": { | ||
"big-integer": "^1.6.43", | ||
"js-sha1": "^0.6.0", | ||
"js-sha256": "^0.9.0" | ||
"big-integer": "^1.6.47" | ||
} | ||
} |
@@ -10,5 +10,6 @@ import pkcs1encrypt from "./pkcs1/encrypt"; | ||
import pkcs1decrypt from "./pkcs1/decrypt"; | ||
import pkcs1sign from "./pkcs1/sign"; | ||
import { arrayBufferToBigInt } from "./bigint"; | ||
import sha1 from "./sha1"; | ||
import sha256 from "./sha256"; | ||
import pkcs1verify from "./pkcs1/verify"; | ||
@@ -23,5 +24,6 @@ export = { | ||
doprivate: pkcs1doprivate, | ||
generate: pkcs1generate | ||
generate: pkcs1generate, | ||
sign: pkcs1sign, | ||
verify: pkcs1verify | ||
}, | ||
sha1: sha1, | ||
sha256: sha256, | ||
@@ -28,0 +30,0 @@ tools: { |
@@ -8,3 +8,3 @@ import { arrayBufferToBigInt } from "../bigint"; | ||
return new Uint8Array(datai.modPow(di, ni).toArray(256).value) | ||
return new Uint8Array(datai.modPow(di, ni).toArray(256).value).buffer | ||
} |
@@ -8,3 +8,3 @@ import { arrayBufferToBigInt } from "../bigint"; | ||
return new Uint8Array(xi.modPow(e, ni).toArray(256).value); | ||
return new Uint8Array(xi.modPow(e, ni).toArray(256).value).buffer; | ||
} |
@@ -8,3 +8,3 @@ import prng from "../rng/prng"; | ||
export function generateRandomBiting(bitlength: number, prng: (ba: TypedArray) => void): BigInt.BigInteger { | ||
const x = new Uint8Array(bitlength / 8 + 1), t = bitlength & 7; | ||
const x = new Uint8Array(bitlength >> 3 + 1), t = bitlength & 7; | ||
prng(x); | ||
@@ -31,3 +31,3 @@ if (t > 0) | ||
const rng = seed ? Alea(seed) : prng(); | ||
const qs = bitlength / 2; | ||
const qs = bitlength >> 1; | ||
const ee = BigInt(expt); | ||
@@ -59,9 +59,9 @@ let p,q; | ||
return { | ||
n: new Uint8Array(n.toArray(256).value), | ||
d: new Uint8Array(d.toArray(256).value), | ||
p: new Uint8Array(p.toArray(256).value), | ||
q: new Uint8Array(q.toArray(256).value), | ||
dmp1: new Uint8Array(dmp1.toArray(256).value), | ||
dmq1: new Uint8Array(dmq1.toArray(256).value), | ||
coeff: new Uint8Array(coeff.toArray(256).value) | ||
n: new Uint8Array(n.toArray(256).value).buffer, | ||
d: new Uint8Array(d.toArray(256).value).buffer, | ||
p: new Uint8Array(p.toArray(256).value).buffer, | ||
q: new Uint8Array(q.toArray(256).value).buffer, | ||
dmp1: new Uint8Array(dmp1.toArray(256).value).buffer, | ||
dmq1: new Uint8Array(dmq1.toArray(256).value).buffer, | ||
coeff: new Uint8Array(coeff.toArray(256).value).buffer | ||
} | ||
@@ -68,0 +68,0 @@ } |
@@ -22,3 +22,3 @@ import prng from "../rng/prng"; | ||
ba.set(new Uint8Array(s), random.byteLength + 3); | ||
return ba; | ||
return ba.buffer; | ||
} |
@@ -5,7 +5,18 @@ import { TypedArray } from "../interface/TypedArray"; | ||
if (typeof window !=="undefined") { | ||
return (ba) => window.crypto.getRandomValues(ba); | ||
// Browser | ||
if (typeof window.crypto !== "undefined") { | ||
return (ba) => window.crypto.getRandomValues(ba); | ||
} else { | ||
return (ba) => { | ||
var l = ba.length | ||
while (l--) { | ||
ba[l] = Math.floor(Math.random() * 256) | ||
} | ||
} | ||
} | ||
} else { | ||
// Node.JS | ||
const crypto = require('crypto'); | ||
return (ba) => crypto.randomFillSync(ba); | ||
} | ||
} | ||
} |
@@ -1,5 +0,133 @@ | ||
const jssha256 = require('js-sha256'); | ||
const K = | ||
[0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, | ||
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, | ||
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, | ||
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, | ||
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, | ||
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, | ||
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, | ||
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2]; | ||
const INIT = | ||
new Uint32Array([0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19]); | ||
const EXTRA = [-2147483648, 8388608, 32768, 128]; | ||
const SHIFT = [24, 16, 8, 0]; | ||
function block(blocks: number[], hash: Uint32Array, frist: boolean) { | ||
let h0 = hash[0], h1 = hash[1], h2 = hash[2], h3 = hash[3], h4 = hash[4], h5 = hash[5], | ||
h6 = hash[6], h7 = hash[7], j, s0, s1, maj, t1, t2, ch, ab, da, cd, bc; | ||
for (j = 16; j < 64; ++j) { | ||
// rightrotate | ||
t1 = blocks[j - 15]; | ||
s0 = ((t1 >>> 7) | (t1 << 25)) ^ ((t1 >>> 18) | (t1 << 14)) ^ (t1 >>> 3); | ||
t1 = blocks[j - 2]; | ||
s1 = ((t1 >>> 17) | (t1 << 15)) ^ ((t1 >>> 19) | (t1 << 13)) ^ (t1 >>> 10); | ||
blocks[j] = blocks[j - 16] + s0 + blocks[j - 7] + s1 << 0; | ||
} | ||
bc = h1 & h2; | ||
for (j = 0; j < 64; j += 4) { | ||
if (frist) { | ||
ab = 704751109; | ||
t1 = blocks[0] - 210244248; | ||
h7 = t1 - 1521486534 << 0; | ||
h3 = t1 + 143694565 << 0; | ||
frist = false; | ||
} else { | ||
s0 = ((h0 >>> 2) | (h0 << 30)) ^ ((h0 >>> 13) | (h0 << 19)) ^ ((h0 >>> 22) | (h0 << 10)); | ||
s1 = ((h4 >>> 6) | (h4 << 26)) ^ ((h4 >>> 11) | (h4 << 21)) ^ ((h4 >>> 25) | (h4 << 7)); | ||
ab = h0 & h1; | ||
maj = ab ^ (h0 & h2) ^ bc; | ||
ch = (h4 & h5) ^ (~h4 & h6); | ||
t1 = h7 + s1 + ch + K[j] + blocks[j]; | ||
t2 = s0 + maj; | ||
h7 = h3 + t1 << 0; | ||
h3 = t1 + t2 << 0; | ||
} | ||
s0 = ((h3 >>> 2) | (h3 << 30)) ^ ((h3 >>> 13) | (h3 << 19)) ^ ((h3 >>> 22) | (h3 << 10)); | ||
s1 = ((h7 >>> 6) | (h7 << 26)) ^ ((h7 >>> 11) | (h7 << 21)) ^ ((h7 >>> 25) | (h7 << 7)); | ||
da = h3 & h0; | ||
maj = da ^ (h3 & h1) ^ ab; | ||
ch = (h7 & h4) ^ (~h7 & h5); | ||
t1 = h6 + s1 + ch + K[j + 1] + blocks[j + 1]; | ||
t2 = s0 + maj; | ||
h6 = h2 + t1 << 0; | ||
h2 = t1 + t2 << 0; | ||
s0 = ((h2 >>> 2) | (h2 << 30)) ^ ((h2 >>> 13) | (h2 << 19)) ^ ((h2 >>> 22) | (h2 << 10)); | ||
s1 = ((h6 >>> 6) | (h6 << 26)) ^ ((h6 >>> 11) | (h6 << 21)) ^ ((h6 >>> 25) | (h6 << 7)); | ||
cd = h2 & h3; | ||
maj = cd ^ (h2 & h0) ^ da; | ||
ch = (h6 & h7) ^ (~h6 & h4); | ||
t1 = h5 + s1 + ch + K[j + 2] + blocks[j + 2]; | ||
t2 = s0 + maj; | ||
h5 = h1 + t1 << 0; | ||
h1 = t1 + t2 << 0; | ||
s0 = ((h1 >>> 2) | (h1 << 30)) ^ ((h1 >>> 13) | (h1 << 19)) ^ ((h1 >>> 22) | (h1 << 10)); | ||
s1 = ((h5 >>> 6) | (h5 << 26)) ^ ((h5 >>> 11) | (h5 << 21)) ^ ((h5 >>> 25) | (h5 << 7)); | ||
bc = h1 & h2; | ||
maj = bc ^ (h1 & h3) ^ cd; | ||
ch = (h5 & h6) ^ (~h5 & h7); | ||
t1 = h4 + s1 + ch + K[j + 3] + blocks[j + 3]; | ||
t2 = s0 + maj; | ||
h4 = h0 + t1 << 0; | ||
h0 = t1 + t2 << 0; | ||
} | ||
hash[0] = hash[0] + h0 << 0; | ||
hash[1] = hash[1] + h1 << 0; | ||
hash[2] = hash[2] + h2 << 0; | ||
hash[3] = hash[3] + h3 << 0; | ||
hash[4] = hash[4] + h4 << 0; | ||
hash[5] = hash[5] + h5 << 0; | ||
hash[6] = hash[6] + h6 << 0; | ||
hash[7] = hash[7] + h7 << 0; | ||
} | ||
export default function sha256(buf: ArrayBuffer): ArrayBuffer { | ||
return jssha256.arrayBuffer(buf) | ||
const h = new Uint32Array(INIT); | ||
const blocks = new Array(17); | ||
blocks.fill(0,1,16); | ||
const bufview = new Uint8Array(buf); | ||
let index = 0, i = 0, hashed = false; | ||
while (index < buf.byteLength) { | ||
blocks.fill(0,1,16); | ||
for (i = 0; index < buf.byteLength && i < 64; index++) { | ||
blocks[i >> 2] |= bufview[index] << SHIFT[i++ & 3]; | ||
} | ||
if (i >= 64) { | ||
block(blocks, h, !hashed); | ||
hashed = true; | ||
} | ||
if (index >= buf.byteLength) { | ||
blocks[i >> 2] |= EXTRA[i & 3]; | ||
if (i >= 56) { | ||
if (i < 64) { | ||
block(blocks, h, !hashed); | ||
hashed = true; | ||
} | ||
blocks[0] = this.block; | ||
blocks.fill(0,1,16); | ||
} | ||
let bitLength = buf.byteLength * 8, hBitLength = 0; | ||
while (bitLength > 4294967295) { | ||
hBitLength += bitLength / 4294967296 << 0; | ||
bitLength = bitLength % 4294967296; | ||
} | ||
blocks[14] = hBitLength; | ||
blocks[15] = bitLength; | ||
block(blocks, h, !hashed); | ||
hashed = true; | ||
} | ||
} | ||
const dataView = new DataView(new ArrayBuffer(32)) | ||
for (let i = 0; i < 8; i++) { | ||
dataView.setUint32(i * 4, h[i]); | ||
} | ||
return dataView.buffer; | ||
} |
import crypto = require('../src/'); | ||
import { compareArrayBuffer } from '../src/utils/arraybuffer'; | ||
@@ -11,4 +12,4 @@ describe('module', () => { | ||
it('padding', () => { | ||
const m = crypto.rsa.padding(new Uint8Array([1,2,3]), 30); | ||
expect(m.length).toBe(30); | ||
const m = new Uint8Array(crypto.rsa.padding(new Uint8Array([1,2,3]), 30)); | ||
expect(m.byteLength).toBe(30); | ||
expect(m[0]).toBe(0); | ||
@@ -30,3 +31,3 @@ expect(m[1]).toBe(2); | ||
const data = new Uint8Array([1,2,3]); | ||
const m = crypto.rsa.padding(data, 30); | ||
const m = new Uint8Array(crypto.rsa.padding(data, 30)); | ||
const unpad = crypto.rsa.unpadding(m, 30); | ||
@@ -49,8 +50,17 @@ expect(data).toEqual(unpad) | ||
it('sign', () => { | ||
const sign = crypto.rsa.sign( | ||
new Uint8Array([1,2,3]), | ||
crypto.tools.hexToArrayBuffer("8e9912f6d3645894e8d38cb58c0db81ff516cf4c7e5a14c7f1eddb1459d2cded4d8d293fc97aee6aefb861859c8b6a3d1dfe710463e1f9ddc72048c09751971c4a580aa51eb523357a3cc48d31cfad1d4a165066ed92d4748fb6571211da5cb14bc11b6e2df7c1a559e6d5ac1cd5c94703a22891464fba23d0d965086277a161"), | ||
crypto.tools.hexToArrayBuffer("a5261939975948bb7a58dffe5ff54e65f0498f9175f5a09288810b8975871e99af3b5dd94057b0fc07535f5f97444504fa35169d461d0d30cf0192e307727c065168c788771c561a9400fb49175e9e6aa4e23fe11af69e9412dd23b0cb6684c4c2429bce139e848ab26d0829073351f4acd36074eafd036a5eb83359d2a698d3"), | ||
) | ||
expect(sign.byteLength * 8).toBe(1024); | ||
}); | ||
it('generate', () => { | ||
const rsa = crypto.rsa.generate(1024, 65537); | ||
expect(rsa.n.length * 8).toBe(1024); | ||
expect(rsa.d.length * 8).toBe(1024); | ||
expect(rsa.p.length * 8).toBe(512); | ||
expect(rsa.q.length * 8).toBe(512); | ||
expect(rsa.n.byteLength * 8).toBe(1024); | ||
expect(rsa.d.byteLength * 8).toBeLessThanOrEqual(1024); | ||
expect(rsa.p.byteLength * 8).toBe(512); | ||
expect(rsa.q.byteLength * 8).toBe(512); | ||
}); | ||
@@ -81,10 +91,2 @@ | ||
describe('sha1', () => { | ||
it('hash', () => { | ||
const hash = crypto.sha1(new Uint8Array([1,2,3])); | ||
expect(hash.byteLength).toBe(20); | ||
expect(hash).toMatchSnapshot(); | ||
}); | ||
}); | ||
describe('sha256', () => { | ||
@@ -94,4 +96,33 @@ it('hash', () => { | ||
expect(hash.byteLength).toBe(32); | ||
expect(hash).toMatchSnapshot(); | ||
expect(new Uint8Array(hash)).toMatchSnapshot(); | ||
}); | ||
describe('benchmark', () => { | ||
const resultA: ArrayBuffer[] = [], resultB: ArrayBuffer[] = []; | ||
const TIMES = 1024; | ||
it('crypto', () => { | ||
const data = []; | ||
for (let i = 0;i < TIMES;i++) { | ||
data.push(i&256); | ||
resultA.push(crypto.sha256(new Uint8Array(data))); | ||
} | ||
}); | ||
it('js-sha256', () => { | ||
const data = []; | ||
for (let i = 0;i < TIMES;i++) { | ||
data.push(i&256); | ||
resultB.push(crypto.sha256(new Uint8Array(data))); | ||
} | ||
}); | ||
it('should same result', () => { | ||
for (let i = 0; i < TIMES; i++) { | ||
const a = resultA[i]; | ||
const b = resultB[i]; | ||
expect(compareArrayBuffer(a,b)).toBe(true); | ||
} | ||
}); | ||
}) | ||
}); |
@@ -0,0 +0,0 @@ { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1
83
1406
83008
5
- Removedjs-sha1@^0.6.0
- Removedjs-sha256@^0.9.0
- Removedjs-sha1@0.6.0(transitive)
- Removedjs-sha256@0.9.0(transitive)
Updatedbig-integer@^1.6.47