diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 50b2fb2..4fc8580 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -19,7 +19,7 @@ jobs: strategy: matrix: os: [ ubuntu-latest, windows-latest ] - extension: [CSV, light,logger, main] + extension: [CSV, light,logger, main, crypto] runs-on: ${{ matrix.os }} defaults: run: @@ -42,7 +42,6 @@ jobs: - uses: actions/setup-node@v1 with: node-version: 14.x - - run: npm config set package-lock false - run: npm install pm2@latest -g; - run: npm install; - run: ./test/CLI/test.sh @@ -64,15 +63,3 @@ jobs: - run: npm install - run: npm start - run: npm test - crypto: - defaults: - run: - working-directory: crypto - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - uses: actions/setup-node@v1 - with: - node-version: 14.x - - run: npm install - - run: npm test diff --git a/crypto/forge/pki.js b/crypto/forge/pki.js deleted file mode 100644 index 33e239b..0000000 --- a/crypto/forge/pki.js +++ /dev/null @@ -1,110 +0,0 @@ -import forge from 'node-forge'; - -const {pki, asn1} = forge; - -const DNSType = 2; - -export class CSR { - - constructor(publicKey, subject, attributes) { - this.initData = { - publicKey, subject, attributes - }; - this.reset(); - } - - //csr.sign = function(key, md) { - // // TODO: get signature OID from private key - // csr.md = md || forge.md.sha1.create(); - // var algorithmOid = oids[csr.md.algorithm + 'WithRSAEncryption']; - // if(!algorithmOid) { - // var error = new Error('Could not compute certification request digest. ' + - // 'Unknown message digest algorithm OID.'); - // error.algorithm = csr.md.algorithm; - // throw error; - // } - // csr.signatureOid = csr.siginfo.algorithmOid = algorithmOid; - // - // // get CertificationRequestInfo, convert to DER - // csr.certificationRequestInfo = pki.getCertificationRequestInfo(csr); - // var bytes = asn1.toDer(csr.certificationRequestInfo); - // - // // digest and sign - // csr.md.update(bytes.getBytes()); - // csr.signature = key.sign(csr.md); - // }; - - toDer() { - const certificationRequestInfo = pki.getCertificationRequestInfo(this.csr); - const bytes = asn1.toDer(certificationRequestInfo); - return bytes.getBytes(); - - } - - reset() { - this.signed = false; - const {publicKey, subject, attributes} = this.initData; - const csr = pki.createCertificationRequest(); - csr.publicKey = publicKey; - const subjectOptions = []; - for (const [key, value] of Object.entries(subject)) { - subjectOptions.push({ - name: key, - value - }); - } - csr.setSubject(subjectOptions); - - const attributeOptions = []; - for (const [name, value] of Object.entries(attributes)) { - if (name === 'extensionRequest') { - const extensions = []; - for (const [_name, _value] of Object.entries(value)) { - if (_name === 'subjectAltName') { - const altNames = _value.map(item => ({type: DNSType, value: item}));// should be array - extensions.push({name: _name, altNames}); - } else { - extensions.push({name: _name, value: _value}); - } - } - attributeOptions.push({name, extensions}); - } else { - attributeOptions.push({name, value}); - } - - } - csr.setAttributes(attributeOptions); - this.csr = csr; - } - - getSignedBy(privateKey) { - this.signed = true; - this.csr.sign(privateKey); - return this.csr; - } - - toString() { - if (this.signed) { - return pki.certificationRequestToPem(this.csr); - } - } - - static fromString(pem) { - return pki.certificationRequestFromPem(pem); - } -} - -export class RSA { - - static generateKeyPair(keySize = 2048) { - const {privateKey, publicKey} = pki.rsa.generateKeyPair(keySize); - return {privateKey, publicKey}; - } -} - - -export class PublicKey { - static fromPEM(pem) { - return pki.publicKeyFromPem(pem) - } -} \ No newline at end of file diff --git a/crypto/package.json b/crypto/package.json index 9a50e9e..806a056 100644 --- a/crypto/package.json +++ b/crypto/package.json @@ -1,9 +1,6 @@ { "name": "@davidkhala/crypto", - "version": "0.0.2", - "engines": { - "node": ">=17" - }, + "version": "0.0.3", "publishConfig": { "access": "public" }, @@ -15,7 +12,6 @@ "@davidkhala/light": "latest" }, "devDependencies": { - "node-forge": "latest", "@davidkhala/logger": "latest", "mocha": "latest", "@davidkhala/milagro-crypto-js": "latest" diff --git a/crypto/test/artifacts/csr.pem b/crypto/test/artifacts/csr.pem index b9dc6e5..92c5463 100644 --- a/crypto/test/artifacts/csr.pem +++ b/crypto/test/artifacts/csr.pem @@ -1,9 +1,9 @@ -----BEGIN CERTIFICATE REQUEST----- -MIIBNjCB3gIBADBNMQswCQYDVQQGEwJISzEUMBIGA1UECgwLSHlwZXJsZWRnZXIx +MIIBODCB3gIBADBNMQswCQYDVQQGEwJISzEUMBIGA1UECgwLSHlwZXJsZWRnZXIx EzARBgNVBAsMCmJsb2NrY2hhaW4xEzARBgNVBAMMCmRhdmlka2hhbGEwWTATBgcq -hkjOPQIBBggqhkjOPQMBBwNCAASdcM8Br5kW835MbO1xFS9RVuGiEln9MUkKzgxY -nK9nBnyuWBClVlV4Zlii2OBeUk2Pso3DXN+Ci/su8WlHmVvAoC8wLQYJKoZIhvcN +hkjOPQIBBggqhkjOPQMBBwNCAAQN/XxpZBSVvdvjzaQxAShwAMozPVYiZHB+RK7I +tfJoqN4DqpDsn4AV7al2P3kWpUzA9O49SN7pPeQfa596x7C+oC8wLQYJKoZIhvcN AQkOMSAwHjAcBgNVHREEFTATghEqLmh5cGVybGVkZ2VyLm9yZzAKBggqhkjOPQQD -AgNHADBEAiBaRmaIt9OgQtghKIUWjSHXAB7W4cmu5BK+7XFYBLnwtgIgWxwbNRCA -Rxs+ncGHwS5a2/jrGevQOfdqwnNn/2WmYqM= +AgNJADBGAiEAkY8HgN5vHbjM/Y5WqPhpvn9n3DnOKEhov8OqObOfUdkCIQDiiWcu +rF93qgKxW3uTvMW7oxph993FsYL2NpQUJ44Bgg== -----END CERTIFICATE REQUEST----- diff --git a/crypto/test/forge/pki.js b/crypto/test/forge/pki.js deleted file mode 100644 index f71667f..0000000 --- a/crypto/test/forge/pki.js +++ /dev/null @@ -1,43 +0,0 @@ -import {CSR, RSA, PublicKey} from '../../forge/pki.js'; -import {chars2Hex} from '@davidkhala/light/format.js'; -import assert from 'assert'; - -describe('csr', () => { - const {privateKey, publicKey} = RSA.generateKeyPair(2048); - const subject = { - commonName: 'oracle', countryName: 'China', localityName: 'HongKong', organizationName: 'hyperledger.org' - }; - const attrs = { - challengePassword: 'password', unstructuredName: 'My Company, Inc.', extensionRequest: { - subjectAltName: ['test.domain.com', 'www.oracle.com', 'www.hyperledger.org'] - } - }; - const csr = new CSR(publicKey, subject, attrs); - it('toString', () => { - - csr.getSignedBy(privateKey); - console.debug('after sign========='); - console.debug(csr.toString()); - }); - it('toDer', () => { - const derBytes = csr.toDer(); - console.info(chars2Hex(derBytes)); - - }); -}); -describe('public key', () => { - it('Error: reading a ecdsa public key pem is not allowed', async () => { - const pem = `-----BEGIN PUBLIC KEY----- -MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEYI7cFGMwqDz17Tywc0bMIZbcIrQP -0QWAvGo+DBLMk5v+zX2C/dHFhgTXhdBI4TnVX6PWv3I6BgVTKEAPxmlW4Q== ------END PUBLIC KEY-----`; - try { - PublicKey.fromPEM(pem); - } catch (e) { - assert.ok(e.message === 'Cannot read public key. Unknown OID.'); - assert.ok(e.oid === '1.2.840.10045.2.1'); - } - - - }); -}); diff --git a/main/format.js b/main/format.js deleted file mode 100644 index 9ba7019..0000000 --- a/main/format.js +++ /dev/null @@ -1,4 +0,0 @@ -import dateFormatter from 'date-format'; - -export const dateFormat = dateFormatter; - diff --git a/main/test/formatTest.js b/main/test/formatTest.js index fcbf37f..7e5fc4f 100644 --- a/main/test/formatTest.js +++ b/main/test/formatTest.js @@ -1,4 +1,4 @@ -import {dateFormat} from '../format.js'; +import dateFormat from 'date-format'; import assert from 'assert'; describe('format test', () => { diff --git a/test/dateformat.mjs b/test/dateformat.mjs index c6ee19d..d86851d 100644 --- a/test/dateformat.mjs +++ b/test/dateformat.mjs @@ -1,10 +1,9 @@ import dateFormat from 'dateformat'; +import assert from 'assert'; -describe('why we not choosing date format', () => { - it('different template pattern', () => { +describe('why we not choosing npm dateformat', () => { + it('case sensitive pattern', () => { const now = new Date(); - - console.debug(dateFormat(now, 'YYMMDDHHMMSSZ')); - console.debug(dateFormat(now, 'yymmddhhmmssz')); + assert.notEqual(dateFormat(now, 'YYYYMMDDHHMMSS'), dateFormat(now, 'yyyymmddhhmmss')); }); }); \ No newline at end of file