Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// obtain local public / secret keys
// message (publicKey1 + publicKey2 + timestamp) signed by the private key of the user represented by publicKey1
let msg;
if (user === 'userA') {
msg = strToUint8Array(
publicKey.toString() + publicKey2.toString() + timestamp,
);
} else if (user === 'userB') {
msg = strToUint8Array(
publicKey2.toString() + publicKey.toString() + timestamp,
);
}
const msgStr = Buffer.from(msg).toString();
const signedMsg = nacl.sign(msg, secretKey);
dispatch(
setPairingMessage({
msg,
msgStr,
signedMsg,
}),
);
// // testing signed message
// const genKeys = nacl.sign.keyPair();
// // console.warn(publicKey.toString());
// console.log(Buffer.from(message).toString());
// const sig1 = nacl.sign.detached(message, secretKey);
// const sig2 = nacl.sign.detached(message, genKeys.secretKey);
// // console.log(publicKey instanceof Uint8Array);
module.exports.sign = function (message/* : Uint8Array */,
secretKey/* : Uint8Array */) {
return nacl.sign(message, secretKey)
}
sign: function (signBuf, detach) {
if (detach) {
//console.log('signed', crypto.createHash('sha1').update(signBuf).digest('hex'))
var sig = Buffer.from(nacl.sign.detached(a(signBuf), a(this.signSk)))
//console.log('sig', crypto.createHash('sha1').update(sig).digest('hex'))
return sig
}
return Buffer(nacl.sign(a(signBuf), a(this.signSk)))
},
regen: function () {
const v = bls.verify(sig, pub, msg)
let end = new Date()
let time = end.getTime() - start.getTime()
console.log('finished in', time, 'ms')
console.log(v)
bls.free(sec)
bls.free(sig)
bls.free(pub)
const keyPair = nacl.sign.keyPair()
start = new Date()
const signedMsg = nacl.sign(msg, keyPair.secretKey)
const rmsg = nacl.sign.open(signedMsg, keyPair.publicKey)
end = new Date()
time = end.getTime() - start.getTime()
console.log('finished in', time, 'ms')
console.log(Buffer.from(rmsg).toString())
})
var message = {
author: keys.publicKey
}
var feed = await localforage.getItem(keys.publicKey)
if (feed) {
var firstMsg = await open(feed[0])
post.seq = ++firstMsg.seq
} else {
post.seq = 1
}
message.key = '%' + nacl.util.encodeBase64(nacl.hash(nacl.util.decodeUTF8(JSON.stringify(post)))),
message.signature = nacl.util.encodeBase64(nacl.sign(nacl.util.decodeUTF8(JSON.stringify(post)), nacl.util.decodeBase64(keys.privateKey)))
var openedMsg = await open(message)
if (!preview) {
console.log('ADDING TO LOG AND FEED')
localforage.getItem('log').then(log => {
if (log) {
log.unshift(openedMsg)
localforage.setItem('log', log)
} else {
var newlog = [openedMsg]
localforage.setItem('log', newlog)
}
})
var subs = [keys.publicKey]
export function sign(msg: Uint8Array, privateKey: Uint8Array): Uint8Array {
const sigMsg = nacl.sign(msg, privateKey)
return sigMsg.slice(0, SIGNATURE_LENGTH)
}
EncryptSES.prototype.encrypt = function (publicKey, message) {
var nonce = nacl.randomBytes(nacl.box.nonceLength);
var msgSign = nacl.sign(message, this.signSecretKey);
var cipher = nacl.box(msgSign, nonce, publicKey, this.boxSecretKey);
return { nonce: nonce, message: nacl.sign(cipher, this.signSecretKey) };
};
return EncryptSES;
getSignedMessage = function(obj, keyBuffer) {
return Buffer.from(nacl.sign(
objToUint8Array(obj),
keyBuffer
)).toString('hex');
},
getMessageSignature = function(obj, keyBuffer) {
encrypt(
publicKey: Uint8Array,
message: Uint8Array
): { message: Uint8Array; nonce: Uint8Array } {
let nonce = nacl.randomBytes(nacl.box.nonceLength);
let msgSign = nacl.sign(message, this.signSecretKey);
let cipher = nacl.box(msgSign, nonce, publicKey, this.boxSecretKey);
return { nonce, message: nacl.sign(cipher, this.signSecretKey) };
}
}
export function sign(msg, privKey) {
const sigMsg = nacl.sign(msg, privKey);
const sig = sigMsg.slice(0, signatureLength);
return new SignatureEd25519(sig);
}