Skip to content

Commit

Permalink
Merge pull request #4 from vMReal/#3-crypto
Browse files Browse the repository at this point in the history
#3 crypto
  • Loading branch information
vMReal authored Aug 6, 2019
2 parents fa84194 + 77160b8 commit 95bd88e
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 9 deletions.
13 changes: 12 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,16 @@
"module": "build/module/index.js",
"repository": "https://github.com/vmreal/digest-auth",
"license": "MIT",
"keywords": ["digest auth", "digest", "basic", "typescript", "node", "axios", "expressjs", "nestjs"],
"keywords": [
"digest auth",
"digest",
"basic",
"typescript",
"node",
"axios",
"expressjs",
"nestjs"
],
"scripts": {
"describe": "npm-scripts-info",
"build": "run-s clean && run-p build:*",
Expand Down Expand Up @@ -52,9 +61,11 @@
"dependencies": {
"class-transformer": "^0.2.0",
"class-validator": "^0.9.1",
"crypto-js": "^3.1.9-1",
"sha.js": "^2.4.11"
},
"devDependencies": {
"@types/crypto-js": "^3.1.43",
"ava": "1.0.0-beta.7",
"codecov": "^3.1.0",
"cz-conventional-changelog": "^2.1.0",
Expand Down
7 changes: 7 additions & 0 deletions src/lib/encryptions/md5.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import test from 'ava';
import { MD5 } from './md5';


test('generate - return string', t => {
t.is(MD5.createHex('test:test'), 'c4f961b4380ee24d982fed76ef36be9f');
});
7 changes: 2 additions & 5 deletions src/lib/encryptions/md5.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import * as crypto from 'crypto';
import cryptoMD5 from 'crypto-js/md5';

export class MD5 {
public static createHex(raw: string): string {
return crypto
.createHash('md5')
.update(raw)
.digest('hex');
return cryptoMD5(raw).toString();
}
}
33 changes: 33 additions & 0 deletions src/lib/encryptions/nonce.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import test from 'ava';
import { Nonce } from './nonce';


test('generate - return string', t => {
t.is(typeof Nonce.generate(), 'string');
t.is(typeof Nonce.generate(10), 'string');
t.is(typeof Nonce.generate(1), 'string');
});


test('generate - default length equal 48', t => {
t.is(Nonce.generate().length, 48);
});


test('generate - custom length equal 20', t => {
t.is(Nonce.generate(20).length, 20);
});

test('generate - custom length equal 2', t => {
t.is(Nonce.generate(2).length, 2);
});


test('generate - custom length equal 1', t => {
t.is(Nonce.generate(1).length, 1);
});

test('generate - custom length lees then 1', t => {
t.is(Nonce.generate(0).length, 0);
t.is(Nonce.generate(-1).length, 0);
});
5 changes: 3 additions & 2 deletions src/lib/encryptions/nonce.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as crypto from 'crypto';
import {lib, enc} from 'crypto-js';

export class Nonce {

Expand All @@ -10,6 +10,7 @@ export class Nonce {
? 1
: length / 2;

return crypto.randomBytes(byteSize).toString('hex').substring(0, length);
return enc.Hex.stringify(lib.WordArray.random(byteSize)).substring(0, length);

}
}

0 comments on commit 95bd88e

Please sign in to comment.