-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #120 from mrstebo/feature/crypto
Added the Crypto faker.
- Loading branch information
Showing
6 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Faker.Crypto | ||
|
||
```js | ||
Faker.Crypto.md5 //=> "6b5ed240042e8a65c55ddb826c3408e6" | ||
|
||
Faker.Crypto.sha1 //=> "4e99e31c51eef8b2d290e709f757f92e558a503f" | ||
|
||
Faker.Crypto.sha256 //=> "51e4dbb424cd9db1ec5fb989514f2a35652ececef33f21c8dd1fd61bb8e3929d" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { itemFromCollection } from '../utils/random'; | ||
import crypto from 'crypto'; | ||
|
||
// 0-9, a-z | ||
const CHARACTERS = [...Array(10).keys()].concat([...Array(26).keys()].map(i => String.fromCharCode(97+i))); | ||
|
||
export function md5() { | ||
const hash = crypto.createHash('md5'); | ||
hash.update(characters(255)); | ||
return hash.digest('hex'); | ||
} | ||
|
||
export function sha1() { | ||
const hash = crypto.createHash('sha1'); | ||
hash.update(characters(255)); | ||
return hash.digest('hex'); | ||
} | ||
|
||
export function sha256() { | ||
const hash = crypto.createHash('sha256'); | ||
hash.update(characters(255)); | ||
return hash.digest('hex'); | ||
} | ||
|
||
function characters(charCount) { | ||
return [...Array(resolveNumber(charCount)).keys()] | ||
.map(_ => itemFromCollection(CHARACTERS)) | ||
.join(''); | ||
} | ||
|
||
function resolveNumber(n) { | ||
return parseInt(n) < 0 ? 0 : parseInt(n); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
'use strict'; | ||
const expect = require('chai').expect; | ||
const Crypto = require('../../src/faker/crypto'); | ||
|
||
describe('Crypto', () => { | ||
describe('#md5', () => { | ||
it('should return a md5', () => { | ||
[...Array(100).keys()].forEach(_ => { | ||
expect(Crypto.md5()).to.match(/^[0-9a-f]+$/); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('#sha1', () => { | ||
it('should return a sha1', () => { | ||
[...Array(100).keys()].forEach(_ => { | ||
expect(Crypto.sha1()).to.match(/^[0-9a-f]+$/); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('#sha256', () => { | ||
it('should return a sha256', () => { | ||
[...Array(100).keys()].forEach(_ => { | ||
expect(Crypto.sha256()).to.match(/^[0-9a-f]+$/); | ||
}); | ||
}); | ||
}); | ||
}); |