Compare commits

...

3 Commits

Author SHA1 Message Date
David Dias
a4e6f9dd83 chore: release version v0.7.3 2016-12-01 11:43:46 +00:00
David Dias
e252db300c chore: update contributors 2016-12-01 11:43:46 +00:00
David Dias
98b37d49c4 feat: add randomBytes function (#45)
* feat: add randomBytes function

* fix: apply CR
2016-12-01 11:42:19 +00:00
4 changed files with 28 additions and 1 deletions

View File

@@ -185,6 +185,11 @@ Converts a private key object into a protobuf serialized private key.
Converts a protobuf serialized private key into its representative object.
### `randomBytes(number)`
- `number: Number`
Generates a Buffer with length `number` populated by random bytes.
## Contribute

View File

@@ -1,6 +1,6 @@
{
"name": "libp2p-crypto",
"version": "0.7.2",
"version": "0.7.3",
"description": "Crypto primitives for libp2p",
"main": "src/index.js",
"browser": {

View File

@@ -71,3 +71,12 @@ exports.marshalPrivateKey = (key, type) => {
return key.bytes
}
exports.randomBytes = (number) => {
if (!number || typeof number !== 'number') {
throw new Error('first argument must be a Number bigger than 0')
}
const buf = new Buffer(number)
c.rsa.getRandomValues(buf)
return buf
}

View File

@@ -97,5 +97,18 @@ describe('libp2p-crypto', () => {
true
)
})
it('randomBytes throws with no number passed', () => {
expect(() => {
crypto.randomBytes()
}).to.throw
})
it('randomBytes', () => {
const buf1 = crypto.randomBytes(10)
expect(buf1.length).to.equal(10)
const buf2 = crypto.randomBytes(10)
expect(buf1).to.not.eql(buf2)
})
})
})