Reproduce a simple encryption test #1
117
index.js
117
index.js
|
@ -1,40 +1,91 @@
|
||||||
var messages = require('bitmessage').messages;
|
|
||||||
var objects = require('bitmessage').objects;
|
// var messages = require('bitmessage').messages;
|
||||||
var Address = require('bitmessage').Address;
|
// var objects = require('bitmessage').objects;
|
||||||
var TcpTransport = require('bitmessage-transports').TcpTransport;
|
// var Address = require('bitmessage').Address;
|
||||||
|
// var TcpTransport = require('bitmessage-transports').TcpTransport;
|
||||||
|
var crypto = require("crypto");
|
||||||
var eccrypto = require("eccrypto");
|
var eccrypto = require("eccrypto");
|
||||||
|
|
||||||
|
var assert = exports.assert = function(condition, message) {
|
||||||
|
if (!condition) {
|
||||||
|
throw new Error(message || "Assertion failed");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
const from = Address.fromPassphrase('loremipsum1');
|
const encPublicKey = Buffer.from(
|
||||||
console.log('[from]', from.encode());
|
'0409d4e5c0ab3d25fe048c64c9da1a242c7f19417e9517cd266950d72c755713585c6178e97fe092fc897c9a1f1720d5770ae8eaad2fa8fcbd08e9324a5dde1857', 'hex'
|
||||||
console.log('[from sign priv key]', from.signPrivateKey.toString('hex'));
|
);
|
||||||
console.log('[from version]', from.version);
|
const message = Buffer.from('The quick brown fox jumps over the lazy dog.');
|
||||||
console.log('[from stream]', from.stream);
|
const ephemPublicKey = Buffer.from(
|
||||||
console.log('[from behaviour]', from.behavior);
|
'02ca00200293213dcf1388b61c2ae5cf80fee6ffffc049a2f9fe7365fe3867813ca812920020df94686c6afb565ac6149b153d61b3b287ee2c7f997c14238796c12b43a3865a', 'hex'
|
||||||
const toAddr = Address.fromPassphrase('gru');
|
);
|
||||||
console.log('[toAddr]', toAddr.encode());
|
const sampleOpts = {
|
||||||
console.log('[toAddr enc pub key]', toAddr.encPublicKey.toString('hex'));
|
iv: Buffer.from('bddb7c2829b08038753084a2f3991681', 'hex'),
|
||||||
console.log('[toAddr version]', toAddr.version);
|
ephemPrivateKey: Buffer.from(
|
||||||
console.log('[toAddr stream]', toAddr.stream);
|
'5be6facd941b76e9d3ead03029fbdb6b6e0809293f7fb197d0c51f84e96b8ba4',
|
||||||
console.log('[toAddr ripe]', toAddr.ripe);
|
'hex'
|
||||||
const encodedMsg = await objects.msg.encodePayloadAsync({
|
)
|
||||||
ttl: 1800,
|
};
|
||||||
from: from,
|
const sampleCiphertext = '64203d5b24688e2547bba345fa139a5a1d962220d4d48a0cf3b1572c0d95b61643a6f9a0d75af7eacc1bd957147bf723';
|
||||||
to: toAddr,
|
const sampleMACKey = 'f83f1e9cc5d6b8448d39dc6a9d5f5b7f460e4a78e9286ee8d91ce1660a53eacd';
|
||||||
message: 'hey there!',
|
const sampleMAC = 'f2526d61b4851fb23409863826fd206165edc021368c7946571cead69046e619';
|
||||||
subject: 'Lorem Ipsum',
|
var mackey = await eccrypto.derive(
|
||||||
encoding: 2,
|
sampleOpts.ephemPrivateKey, encPublicKey
|
||||||
friend: true,
|
).then(function(Px) {
|
||||||
skipPow: true,
|
var hash = crypto.createHash("sha512").update(Px).digest();
|
||||||
|
// var encryptionKey = hash.slice(0, 32);
|
||||||
|
var macKey = hash.slice(32);
|
||||||
|
return macKey
|
||||||
});
|
});
|
||||||
const strippedMsg = encodedMsg.slice(8);
|
console.log('[mackey]', mackey.toString('hex'));
|
||||||
console.log('[check messages]', strippedMsg);
|
|
||||||
console.log('[check messages]', strippedMsg.toString('hex'));
|
assert(mackey.toString('hex') == sampleMACKey, 'Bad MAC key!')
|
||||||
var d = await objects.msg.decodePayloadAsync(encodedMsg, {
|
|
||||||
skipPow: true,
|
var alt_mac = crypto.createHmac("sha256", mackey).update(
|
||||||
identities: toAddr,
|
Buffer.concat([
|
||||||
});
|
sampleOpts.iv, ephemPublicKey, Buffer.from(sampleCiphertext, 'hex')])
|
||||||
console.log('[decoded]', d.message);
|
).digest()
|
||||||
|
|
||||||
|
console.log('[alt mac]', alt_mac.toString('hex'));
|
||||||
|
|
||||||
|
var encrypted = await eccrypto.encrypt(encPublicKey, message, sampleOpts);
|
||||||
|
console.log('[ephem pubkey]', encrypted.ephemPublicKey.toString('hex'));
|
||||||
|
console.log('[ciphertext]', encrypted.ciphertext.toString('hex'));
|
||||||
|
console.log('[mac]', encrypted.mac.toString('hex'));
|
||||||
|
|
||||||
|
assert(encrypted.ciphertext.toString('hex') == sampleCiphertext, 'Bad ciphertext!');
|
||||||
|
assert(encrypted.mac.toString('hex') == sampleMAC, 'Bad MAC!');
|
||||||
|
// const from = Address.fromPassphrase('loremipsum1');
|
||||||
|
// console.log('[from]', from.encode());
|
||||||
|
// console.log('[from sign priv key]', from.signPrivateKey.toString('hex'));
|
||||||
|
// console.log('[from version]', from.version);
|
||||||
|
// console.log('[from stream]', from.stream);
|
||||||
|
// console.log('[from behaviour]', from.behavior);
|
||||||
|
// const toAddr = Address.fromPassphrase('gru');
|
||||||
|
// console.log('[toAddr]', toAddr.encode());
|
||||||
|
// console.log('[toAddr enc pub key]', toAddr.encPublicKey.toString('hex'));
|
||||||
|
// console.log('[toAddr version]', toAddr.version);
|
||||||
|
// console.log('[toAddr stream]', toAddr.stream);
|
||||||
|
// console.log('[toAddr ripe]', toAddr.ripe);
|
||||||
|
// const encodedMsg = await objects.msg.encodePayloadAsync({
|
||||||
|
// ttl: 1800,
|
||||||
|
// from: from,
|
||||||
|
// to: toAddr,
|
||||||
|
// message: 'hey there!',
|
||||||
|
// subject: 'Lorem Ipsum',
|
||||||
|
// encoding: 2,
|
||||||
|
// friend: true,
|
||||||
|
// skipPow: true,
|
||||||
|
// });
|
||||||
|
// const strippedMsg = encodedMsg.slice(8);
|
||||||
|
// console.log('[check messages]', strippedMsg);
|
||||||
|
// console.log('[check messages]', strippedMsg.toString('hex'));
|
||||||
|
// var d = await objects.msg.decodePayloadAsync(encodedMsg, {
|
||||||
|
// skipPow: true,
|
||||||
|
// identities: toAddr,
|
||||||
|
// });
|
||||||
|
// console.log('[decoded]', d.message);
|
||||||
// const encPublicKey = '04a60f6cd97ecd16768215ef059ccfbb1840b736bcdb0763872ddfeef4c417d2ec8439383ce68de9a2b30ad44e9f0e56a13fc2895a6e41f7cf3757e46181d32dd0';
|
// const encPublicKey = '04a60f6cd97ecd16768215ef059ccfbb1840b736bcdb0763872ddfeef4c417d2ec8439383ce68de9a2b30ad44e9f0e56a13fc2895a6e41f7cf3757e46181d32dd0';
|
||||||
// const encPrivateKey = '2cb5ae4e8cf8c71d1221f5c6c46810b448207cacedd86cc52219c3f107048c10';
|
// const encPrivateKey = '2cb5ae4e8cf8c71d1221f5c6c46810b448207cacedd86cc52219c3f107048c10';
|
||||||
// const dec = await objects.msg.decPayloadTest(encPrivateKey, e);
|
// const dec = await objects.msg.decPayloadTest(encPrivateKey, e);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user