@eyhn/crypto
Advanced tools
Comparing version 1.0.9 to 1.0.10
@@ -14,2 +14,4 @@ import pkcs1encrypt from "./pkcs1/encrypt"; | ||
import pkcs1verify from "./pkcs1/verify"; | ||
import aesctrencrypt from "./aes/ctrencrypt"; | ||
import prng from "./rng/prng"; | ||
declare const _default: { | ||
@@ -28,2 +30,9 @@ rsa: { | ||
sha256: typeof sha256; | ||
aes: { | ||
ctr: { | ||
encrypt: typeof aesctrencrypt; | ||
decrypt: typeof aesctrencrypt; | ||
}; | ||
}; | ||
prng: typeof prng; | ||
tools: { | ||
@@ -30,0 +39,0 @@ hexToArrayBuffer: typeof hexToArrayBuffer; |
@@ -15,2 +15,4 @@ "use strict"; | ||
const verify_1 = require("./pkcs1/verify"); | ||
const ctrencrypt_1 = require("./aes/ctrencrypt"); | ||
const prng_1 = require("./rng/prng"); | ||
module.exports = { | ||
@@ -29,2 +31,9 @@ rsa: { | ||
sha256: sha256_1.default, | ||
aes: { | ||
ctr: { | ||
encrypt: ctrencrypt_1.default, | ||
decrypt: ctrencrypt_1.default | ||
} | ||
}, | ||
prng: prng_1.default, | ||
tools: { | ||
@@ -31,0 +40,0 @@ hexToArrayBuffer: hex_1.hexToArrayBuffer, |
import { TypedArray } from "../interface/TypedArray"; | ||
export default function prng(): (ba: TypedArray) => void; | ||
export default function prng(): (ba: TypedArray) => TypedArray; |
@@ -15,2 +15,3 @@ "use strict"; | ||
} | ||
return ba; | ||
}; | ||
@@ -21,4 +22,4 @@ } | ||
// Node.JS | ||
const crypto = require('crypto'); | ||
return (ba) => crypto.randomFillSync(ba); | ||
const nodecrypto = require('crypto'); | ||
return (ba) => nodecrypto.randomFillSync(ba); | ||
} | ||
@@ -25,0 +26,0 @@ } |
{ | ||
"name": "@eyhn/crypto", | ||
"version": "1.0.9", | ||
"version": "1.0.10", | ||
"main": "lib/index.js", | ||
@@ -9,2 +9,3 @@ "types": "lib/index.d.ts", | ||
"license": "MIT", | ||
"sideEffects": false, | ||
"scripts": { | ||
@@ -15,4 +16,6 @@ "test": "jest --config jest.config.js", | ||
"devDependencies": { | ||
"@types/aes-js": "^3.1.0", | ||
"aes-js": "^3.1.2", | ||
"@types/jest": "^24.0.20", | ||
"@types/node": "^12.11.7", | ||
"@types/jest": "^24.0.20", | ||
"jest": "^24.9.0", | ||
@@ -19,0 +22,0 @@ "ts-jest": "^24.1.0", |
@@ -14,2 +14,4 @@ import pkcs1encrypt from "./pkcs1/encrypt"; | ||
import pkcs1verify from "./pkcs1/verify"; | ||
import aesctrencrypt from "./aes/ctrencrypt"; | ||
import prng from "./rng/prng"; | ||
@@ -29,2 +31,9 @@ export = { | ||
sha256: sha256, | ||
aes: { | ||
ctr: { | ||
encrypt: aesctrencrypt, | ||
decrypt: aesctrencrypt | ||
} | ||
}, | ||
prng: prng, | ||
tools: { | ||
@@ -31,0 +40,0 @@ hexToArrayBuffer: hexToArrayBuffer, |
import { TypedArray } from "../interface/TypedArray"; | ||
import * as crypto from 'crypto'; | ||
export default function prng(): (ba: TypedArray) => void { | ||
export default function prng(): (ba: TypedArray) => TypedArray { | ||
if (typeof window !=="undefined") { | ||
@@ -14,2 +15,3 @@ // Browser | ||
} | ||
return ba; | ||
} | ||
@@ -19,5 +21,5 @@ } | ||
// Node.JS | ||
const crypto = require('crypto'); | ||
return (ba) => crypto.randomFillSync(ba); | ||
const nodecrypto: typeof crypto = require('crypto'); | ||
return (ba) => nodecrypto.randomFillSync(ba); | ||
} | ||
} |
@@ -1,3 +0,3 @@ | ||
import crypto = require('../src/'); | ||
import { compareArrayBuffer } from '../src/utils/arraybuffer'; | ||
import crypto = require('../lib/'); | ||
import { compareArrayBuffer } from '../lib/utils/arraybuffer'; | ||
@@ -124,1 +124,56 @@ describe('module', () => { | ||
}); | ||
describe('aes', () => { | ||
const aesjs = require('aes-js'); | ||
const plaintext = Array(100).fill('Hello world').join(','); | ||
it('aes-ctr-256', () => { | ||
const prng = crypto.prng(); | ||
const key = prng(new Uint8Array(256 / 8)).buffer; | ||
const aesjsctr = new aesjs.ModeOfOperation.ctr(new Uint8Array(key), new aesjs.Counter(1)); | ||
const ciphertext = crypto.aes.ctr.encrypt(key, crypto.tools.textToArrayBuffer(plaintext), 1); | ||
expect(compareArrayBuffer( | ||
ciphertext, | ||
aesjsctr.encrypt(aesjs.utils.utf8.toBytes(plaintext)).buffer | ||
)).toBe(true); | ||
expect( | ||
crypto.tools.arrayBufferToText(crypto.aes.ctr.decrypt(key, ciphertext, 1)) | ||
).toEqual(plaintext); | ||
}); | ||
describe('benchmark', () => { | ||
const TIMES = 256; | ||
const key = crypto.prng()(new Uint8Array(256 / 8)).buffer; | ||
const resultA: ArrayBuffer[] = [], resultB: ArrayBuffer[] = []; | ||
it('crypto', () => { | ||
for (let i = 0; i < TIMES; i++) { | ||
resultA.push( | ||
crypto.aes.ctr.encrypt(key, crypto.tools.textToArrayBuffer(plaintext), 1) | ||
); | ||
} | ||
}); | ||
it('ase-js', () => { | ||
for (let i = 0; i < TIMES; i++) { | ||
const aesjsctr = new aesjs.ModeOfOperation.ctr(new Uint8Array(key), new aesjs.Counter(1)) | ||
const ciphertext = aesjsctr.encrypt(aesjs.utils.utf8.toBytes(plaintext)).buffer | ||
resultB.push( | ||
ciphertext | ||
); | ||
} | ||
}); | ||
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); | ||
} | ||
}); | ||
}) | ||
}) |
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
195181
110
1957
7