mirror of
https://github.com/fluencelabs/js-libp2p-crypto
synced 2025-07-25 17:32:17 +00:00
Compare commits
10 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
fba4d3cc0f | ||
|
d8f8717a16 | ||
|
6b4a1ab7a3 | ||
|
18810aca86 | ||
|
1f4823e202 | ||
|
c33530186a | ||
|
87a30e2e9b | ||
|
3037541d3c | ||
|
c586a8825b | ||
|
b07bca569c |
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "js-libp2p-crypto",
|
"name": "libp2p-crypto",
|
||||||
"version": "0.2.0",
|
"version": "0.4.0",
|
||||||
"description": "Crypto primitives for libp2p",
|
"description": "Crypto primitives for libp2p",
|
||||||
"main": "lib/index.js",
|
"main": "lib/index.js",
|
||||||
"jsnext:main": "src/index.js",
|
"jsnext:main": "src/index.js",
|
||||||
|
@@ -30,7 +30,7 @@ module.exports = (curveName) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
key: priv.getPublic(),
|
key: new Buffer(priv.getPublic('hex'), 'hex'),
|
||||||
genSharedKey
|
genSharedKey
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -21,7 +21,8 @@ const cipherMap = {
|
|||||||
const hashMap = {
|
const hashMap = {
|
||||||
SHA1: 'sha1',
|
SHA1: 'sha1',
|
||||||
SHA256: 'sha256',
|
SHA256: 'sha256',
|
||||||
SHA512: 'sha512'
|
// workaround for https://github.com/digitalbazaar/forge/issues/401
|
||||||
|
SHA512: forge.md.sha512.create()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generates a set of keys for each party by stretching the shared key.
|
// Generates a set of keys for each party by stretching the shared key.
|
||||||
|
@@ -19,7 +19,11 @@ class RsaPublicKey {
|
|||||||
|
|
||||||
verify (data, sig) {
|
verify (data, sig) {
|
||||||
const md = forge.md.sha256.create()
|
const md = forge.md.sha256.create()
|
||||||
md.update(data, 'utf8')
|
if (Buffer.isBuffer(data)) {
|
||||||
|
md.update(data.toString('binary'), 'binary')
|
||||||
|
} else {
|
||||||
|
md.update(data)
|
||||||
|
}
|
||||||
|
|
||||||
return this._key.verify(md.digest().bytes(), sig)
|
return this._key.verify(md.digest().bytes(), sig)
|
||||||
}
|
}
|
||||||
@@ -60,9 +64,13 @@ class RsaPrivateKey {
|
|||||||
|
|
||||||
sign (message) {
|
sign (message) {
|
||||||
const md = forge.md.sha256.create()
|
const md = forge.md.sha256.create()
|
||||||
md.update(message, 'utf8')
|
if (Buffer.isBuffer(message)) {
|
||||||
|
md.update(message.toString('binary'), 'binary')
|
||||||
return this._privateKey.sign(md)
|
} else {
|
||||||
|
md.update(message)
|
||||||
|
}
|
||||||
|
const raw = this._privateKey.sign(md, 'RSASSA-PKCS1-V1_5')
|
||||||
|
return new Buffer(raw, 'binary')
|
||||||
}
|
}
|
||||||
|
|
||||||
get public () {
|
get public () {
|
||||||
|
@@ -9,8 +9,7 @@ const fixtures = require('./fixtures/go-stretch-key')
|
|||||||
describe('keyStretcher', () => {
|
describe('keyStretcher', () => {
|
||||||
describe('generate', () => {
|
describe('generate', () => {
|
||||||
const ciphers = ['AES-128', 'AES-256', 'Blowfish']
|
const ciphers = ['AES-128', 'AES-256', 'Blowfish']
|
||||||
const hashes = ['SHA1', 'SHA256']
|
const hashes = ['SHA1', 'SHA256', 'SHA512']
|
||||||
// add 'SHA512' when https://github.com/digitalbazaar/forge/issues/401 is resolved
|
|
||||||
const res = crypto.generateEphemeralKeyPair('P-256')
|
const res = crypto.generateEphemeralKeyPair('P-256')
|
||||||
const secret = res.genSharedKey(res.key)
|
const secret = res.genSharedKey(res.key)
|
||||||
|
|
||||||
|
@@ -112,4 +112,26 @@ describe('RSA', () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('sign and verify', () => {
|
||||||
|
const data = new Buffer('hello world')
|
||||||
|
const sig = key.sign(data)
|
||||||
|
|
||||||
|
expect(
|
||||||
|
key.public.verify(data, sig)
|
||||||
|
).to.be.eql(
|
||||||
|
true
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('does fails to verify for different data', () => {
|
||||||
|
const data = new Buffer('hello world')
|
||||||
|
const sig = key.sign(data)
|
||||||
|
|
||||||
|
expect(
|
||||||
|
key.public.verify(new Buffer('hello'), sig)
|
||||||
|
).to.be.eql(
|
||||||
|
false
|
||||||
|
)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user