mirror of
https://github.com/fluencelabs/js-libp2p-crypto
synced 2025-03-15 19:31:05 +00:00
feat: add randomBytes function (#45)
* feat: add randomBytes function * fix: apply CR
This commit is contained in:
parent
f979fcd3c2
commit
98b37d49c4
@ -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.
|
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
|
## Contribute
|
||||||
|
|
||||||
|
@ -71,3 +71,12 @@ exports.marshalPrivateKey = (key, type) => {
|
|||||||
|
|
||||||
return key.bytes
|
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
|
||||||
|
}
|
||||||
|
@ -97,5 +97,18 @@ describe('libp2p-crypto', () => {
|
|||||||
true
|
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)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user