From 0c01420c1e14992a4169098ccd47cd196b899f06 Mon Sep 17 00:00:00 2001 From: nanov Date: Tue, 5 Nov 2019 06:09:07 +0200 Subject: [PATCH] chore: cleanup --- lib/keymanagers/Keys.js | 1 - lib/keymanagers/_Keys.js | 59 -------- lib/keymanagers/keyRSA.js | 228 ------------------------------ lib/orders/H004/response.js | 1 - package.json | 2 +- test/exponentHandle.js | 44 ------ test/{ => unit}/keys.js | 6 +- test/{ => unit}/responseParser.js | 2 +- 8 files changed, 6 insertions(+), 337 deletions(-) delete mode 100644 lib/keymanagers/_Keys.js delete mode 100644 lib/keymanagers/keyRSA.js delete mode 100644 test/exponentHandle.js rename test/{ => unit}/keys.js (94%) rename test/{ => unit}/responseParser.js (96%) diff --git a/lib/keymanagers/Keys.js b/lib/keymanagers/Keys.js index 1762642..0250c1c 100644 --- a/lib/keymanagers/Keys.js +++ b/lib/keymanagers/Keys.js @@ -1,6 +1,5 @@ 'use strict'; -// const Key = require('./keyRSA'); const Key = require('./Key'); const keyOrNull = (key) => { diff --git a/lib/keymanagers/_Keys.js b/lib/keymanagers/_Keys.js deleted file mode 100644 index 94fb1ce..0000000 --- a/lib/keymanagers/_Keys.js +++ /dev/null @@ -1,59 +0,0 @@ -'use strict'; - -const Key = require('./keyRSA'); - -const keyOrNull = key => (key ? Key(key) : null); - -module.exports = class Keys { - constructor({ - A006, - E002, - X002, - bankX002, - bankE002, - }) { - this.keys = { - A006: keyOrNull(A006), - E002: keyOrNull(E002), - X002: keyOrNull(X002), - bankX002: keyOrNull(bankX002), - bankE002: keyOrNull(bankE002), - }; - console.log('debug'); - } - - static generate() { - const keys = {}; - - Object.keys({ A006: '', X002: '', E002: '' }).forEach((key) => { - keys[key] = Key().generate(); - }); - - return new Keys(keys); - } - - setBankKeys(bankKeys) { - this.keys.bankX002 = Key().importKey(bankKeys.bankX002); - this.keys.bankE002 = Key().importKey(bankKeys.bankE002); - } - - a() { - return this.keys.A006; - } - - e() { - return this.keys.E002; - } - - x() { - return this.keys.X002; - } - - bankX() { - return this.keys.bankX002; - } - - bankE() { - return this.keys.bankE002; - } -}; diff --git a/lib/keymanagers/keyRSA.js b/lib/keymanagers/keyRSA.js deleted file mode 100644 index e359329..0000000 --- a/lib/keymanagers/keyRSA.js +++ /dev/null @@ -1,228 +0,0 @@ -'use strict'; - -/* eslint-disable camelcase */ - -function rsaPublicKeyPem(modulus_b64, exponent_b64) { - function prepadSigned(hexStr) { - const msb = hexStr[0]; - if ( - (msb >= '8' && msb <= '9') || - (msb >= 'a' && msb <= 'f') || - (msb >= 'A' && msb <= 'F')) - return `00${hexStr}`; - - return hexStr; - } - - function toHex(number) { - const nstr = number.toString(16); - if (nstr.length % 2 === 0) return nstr; - return `0${nstr}`; - } - - // encode ASN.1 DER length field - // if <=127, short from - // if >=128, long from - function encodeLengthHex(n) { - if (n <= 127) return toHex(n); - - const n_hex = toHex(n); - const length_of_length_byte = 128 + (n_hex.length / 2); // 0x80+numbytes - return toHex(length_of_length_byte) + n_hex; - } - - const modulus = Buffer.from(modulus_b64, 'base64'); - const exponent = Buffer.from(exponent_b64, 'base64'); - - let modulus_hex = modulus.toString('hex'); - let exponent_hex = exponent.toString('hex'); - - modulus_hex = prepadSigned(modulus_hex); - exponent_hex = prepadSigned(exponent_hex); - - const modlen = modulus_hex.length / 2; - const explen = exponent_hex.length / 2; - - const encoded_modlen = encodeLengthHex(modlen); - const encoded_explen = encodeLengthHex(explen); - const encoded_pubkey = `30${ - encodeLengthHex(modlen + explen + (encoded_modlen.length / 2) + (encoded_explen.length / 2) + 2) - }02${encoded_modlen}${modulus_hex - }02${encoded_explen}${exponent_hex}`; - - let seq2 = - `${'30 0d ' + - '06 09 2a 86 48 86 f7 0d 01 01 01' + - '05 00 ' + - '03'}${encodeLengthHex((encoded_pubkey.length / 2) + 1) - }00${encoded_pubkey}`; - - seq2 = seq2.replace(/ /g, ''); - - let der_hex = `30${encodeLengthHex(seq2.length / 2)}${seq2}`; - - der_hex = der_hex.replace(/ /g, ''); - - const der = Buffer.from(der_hex, 'hex'); - const der_b64 = der.toString('base64'); - - const pem = `-----BEGIN PUBLIC KEY-----\n${ - der_b64.match(/.{1,64}/g).join('\n') - }\n-----END PUBLIC KEY-----\n`; - - return pem.trim(); -} - -const BN = require('bn.js'); -const NodeRSA = require('node-rsa'); - -const { - pki: { - rsa, - publicKeyToPem, - privateKeyToPem, - publicKeyFromPem, - privateKeyFromPem, - }, - jsbn: { - BigInteger, - }, -} = require('node-forge'); - -const isKeyInstance = (obj) => { - if (typeof obj !== 'object') - return false; - return ('publicKey' in obj && 'privateKey' in obj); -}; - -const getKeyType = (str) => { - const matches = str.match(/(PRIVATE|PUBLIC) KEY/); - if (!matches) - return null; - return matches[1].toLowerCase(); -}; - -/* -class RsaKeyPair { - constructor() { - this._isPublic = null; - this._publicKey = null; - this._privateKey = null; - } - fromString(str) { - - } -} -*/ - -const keyOrNull = (encodedKey) => { - if (encodedKey === null) return {}; - if (typeof encodedKey === 'string') { - const type = getKeyType(encodedKey); - const isPublic = type === 'public'; - const key = isPublic ? publicKeyFromPem(encodedKey) : privateKeyFromPem(encodedKey); - key.isPublic = isPublic; - return key; - } - - return encodedKey; - // return (isKeyInstance(encodedKey)) ? encodedKey; - - /* const k = (encodedKey instanceof NodeRSA) ? encodedKey : new NodeRSA(encodedKey); - if (k.keyPair.e === 16777216) - k.keyPair.e = 4294967311; - return k; */ -}; - -module.exports = (encodedKey) => { - if (encodedKey && encodedKey.__RsaKey) - return encodedKey; - return { - __RsaKey: true, - key: keyOrNull(encodedKey), - - generate(keySize = 2048) { - const keyPair = rsa.generateKeyPair(keySize); - this.key = keyPair.privateKey; - this.key.isPublic = false; - this.publicKey = keyPair.publicKey; - return this; - // return rsa.generateKeyPair(keySize); - // return new NodeRSA({ b: keySize }); - }, - - importKey({ - mod, - exp, - modulus, - exponent, - }) { - this.key = rsa.setPublicKey(new BigInteger(mod.toString('hex'), 16), new BigInteger(exp.toString('hex'), 16)); - this.key.isPublic = true; - // const k = rsa.generateKeyPair(); - // k.publicKey = rsa.setPublicKey(mod, exp); - // this.key = k; - // this.key.publicKey. - - // .this.key.importKey({ n: mod, e: exp }, 'components-public'); - /* - this.pempem = modulus && exponent ? { - modulus, - exponent, - } : null; - */ - - return this; - }, - - n(to = 'buff') { - const key = this.publicKey || this.key; - const keyN = Buffer.from(key.n.toByteArray()); - - return to === 'hex' - ? keyN.toString('hex', 1) - : keyN; - }, - - e(to = 'buff') { - const key = this.publicKey || this.key; - const eKey = Buffer.from(key.e.toByteArray()); // new BN(this.key.exportKey('components-public').e).toBuffer(); - - return to === 'hex' - ? eKey.toString('hex') - : eKey; - }, - - d() { - return Buffer.from(this.key.d.toByteArray()); - // return this.key.keyPair.d.toBuffer(); - }, - - isPrivate() { - return !this.key.isPublic; - }, - - isPublic() { - return this.key.isPublic; - // return this.key.isPublic(); - }, - - size() { - return 2048; - // return this.key.getKeySize(); - }, - - toPem() { - if (this.isPublic()) - return publicKeyToPem(this.key); - return privateKeyToPem(this.key); - /* - if (this.pempem) - return rsaPublicKeyPem(this.pempem.modulus, this.pempem.exponent); - const isPrivate = this.key.isPrivate(); - const pem = isPrivate ? this.key.exportKey('pkcs1-private-pem') : this.key.exportKey('pkcs8-public-pem'); - return pem; - */ - }, - }; -}; diff --git a/lib/orders/H004/response.js b/lib/orders/H004/response.js index b82a9ab..b7344fa 100644 --- a/lib/orders/H004/response.js +++ b/lib/orders/H004/response.js @@ -2,7 +2,6 @@ const zlib = require('zlib'); const crypto = require('crypto'); -const BN = require('bn.js'); const Crypto = require('../../crypto/Crypto'); diff --git a/package.json b/package.json index 4427452..4484da1 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "index.js", "scripts": { "lint": "eslint .", - "test": "mocha test/*/**.js" + "test": "mocha test/**/*.js" }, "repository": { "type": "git", diff --git a/test/exponentHandle.js b/test/exponentHandle.js deleted file mode 100644 index 1f85741..0000000 --- a/test/exponentHandle.js +++ /dev/null @@ -1,44 +0,0 @@ -'use strict'; - -const { assert } = require('chai'); -const H004Response = require('../lib/orders/H004/response'); - -describe('H004 response parsing', () => { - it('parses bank keys exp', () => { - const response = H004Response('', {}); - - const x002mod = 'AJ2/0OoIZydb9sgKbiwqDcwA0NtcUMIYi6GK7PqoRszu1uytGnxJjQhGt62kMslWvLgebUSwdq/T3YyGsa3KQeIGaUUn9iqyu3BoNOMdo2DLN4NdGMY1WR/HbYRKR3JHyURhTBKw27KScRRdKGl6jY+VcXcTKJL8PjXMQH5cD6Gz'; // 'ALr6oSdqbbpRxlJmKtwTjjaCA1zlA61Vd7NfPML1NxY/U3Dt8galrieRjBM0SDn4vD2+AJCQY4zzkdE2m8u/p+3KGtmZtGaaxs11TNRm9GhtwQGw2hW73RduTSwLZJrSilv/GH18vPar8uvlBAXTmtVEy2kfvT3+t1mvqrut/LnUg3t63nx3WVdWylEdVDmf9Ylp+W+2quLyE0TzTVl5OXhvB3tpVs2B4OfsmOHJAaKIdSuXDMJebPiM9uIlraiJVeRMjkW3Xxmrazc7+kuy8RZ4BBgi+Xve6lNaQHGqxPV5q5SeXR5fS0D2sPeewJWbjhaVRBObIV2ZEoEWKx79pnE='; - const e002mod = 'AOzWaiT7aGESXcI3dqLY3RRD36inlZTGmNNprKd/t9uHfoMeLwZHeMwtjCRWjsuZEyBupkNSFWb3vBlxDyhcyTgpbbbcHsDGqF2zCJaK85xUphoH9mKHxbnA8ZlXzmtHwDmwVSns0FAslIqD+Xr+WycQpeCBEK12D8Ii032YS814ZUKHJ1MkS65A5PE0lcvMTyIE7ruG1kFz85F4nX8eWq77mDEiBONkA5RSUb5duGnRohdNYBgO8K6Wn18aDdISGDyPyXHNvC70v8tfWbF9VGE3rXVGcgjpezZZxC8d47vL0x6lOeslgl7s8N456ntAa+oGHRurt5mEhDz1DZg+EJc='; - const exponent = 'AQAB'; // 'AQAAAA8='; - - response.orderData = () => ` - - - - - ${x002mod} - ${exponent} - - 2015-02-25T08:01:13.061Z - - X002 - - - - - ${e002mod} - ${exponent} - - 2015-02-25T08:01:12.344Z - - E002 - - SBKPR01 - `; - - const bankKeys = response.bankKeys(); - - assert.equal(bankKeys.bankX002.mod.toString('base64'), x002mod); - assert.equal(bankKeys.bankE002.mod.toString('base64'), e002mod); - }); -}); diff --git a/test/keys.js b/test/unit/keys.js similarity index 94% rename from test/keys.js rename to test/unit/keys.js index b911f3d..8bf3aac 100644 --- a/test/keys.js +++ b/test/unit/keys.js @@ -1,9 +1,11 @@ 'use strict'; +/* eslint-env node, mocha */ + const { assert } = require('chai'); -const fixtures = require('./fixtures/keys'); +const fixtures = require('../fixtures/keys'); // const Key = require('../lib/keymanagers/keyRSA'); -const Key = require('../lib/keymanagers/Key'); +const Key = require('../../lib/keymanagers/Key'); const stripWhitespace = str => str.replace(/\s+/g, ''); diff --git a/test/responseParser.js b/test/unit/responseParser.js similarity index 96% rename from test/responseParser.js rename to test/unit/responseParser.js index d561bdb..6d65034 100644 --- a/test/responseParser.js +++ b/test/unit/responseParser.js @@ -3,7 +3,7 @@ /* eslint-env node, mocha */ const { assert } = require('chai'); -const H004Response = require('../lib/orders/H004/response'); +const H004Response = require('../../lib/orders/H004/response'); describe('H004 response parsing', () => { it('parses bank keys', () => {