-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
79 lines (63 loc) · 3.2 KB
/
Copy pathindex.js
File metadata and controls
79 lines (63 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const { keyhelper, SessionCipher, ProtocolAddress } = require("libsignal");
const SessionBuilder = require("libsignal/src/session_builder");
const Store = require("./store");
const decryptMessage = (sessionCipher, encryptedMessage) => {
if (encryptedMessage.type === 3) {
return sessionCipher.decryptPreKeyWhisperMessage(encryptedMessage.body)
}
return sessionCipher.decryptWhisperMessage(encryptedMessage.body)
}
const generateKeyBundle = (regId, identityKey, signedPreKey, preKey) => {
return {
registrationId: regId,
identityKey: identityKey.pubKey,
signedPreKey: {
keyId: signedPreKey.keyId,
publicKey: signedPreKey.keyPair.pubKey,
signature: signedPreKey.signature,
},
preKey: {
keyId: preKey.keyId,
publicKey: preKey.keyPair.pubKey
}
}
}
// Initialization of Alice store
const aliceStore = new Store();
const aliceIdentityKeyPair = keyhelper.generateIdentityKeyPair()
const aliceRegId = keyhelper.generateRegistrationId();
const alicePreKey = keyhelper.generatePreKey(Math.floor(Math.random() * 27));
const aliceSignedPrekey = keyhelper.generateSignedPreKey(aliceIdentityKeyPair, Math.floor(Math.random() * 28))
aliceStore.storeRegistrationId(aliceRegId)
aliceStore.storeIdentityKey(aliceIdentityKeyPair)
aliceStore.storePreKey(alicePreKey.keyId, alicePreKey);
aliceStore.storeSignedPreKey(aliceSignedPrekey.keyId, aliceSignedPrekey)
const ALICE_ADDRESS = new ProtocolAddress('+393278994952', 1)
// Initialization of Bob store
const bobStore = new Store();
const bobIdentityKeyPair = keyhelper.generateIdentityKeyPair()
const bobRegId = keyhelper.generateRegistrationId();
const bobPreKey = keyhelper.generatePreKey(Math.floor(Math.random() * 28));
const bobSignedPrekey = keyhelper.generateSignedPreKey(bobIdentityKeyPair, Math.floor(Math.random() * 28))
bobStore.storeIdentityKey(bobIdentityKeyPair)
bobStore.storeRegistrationId(bobRegId)
bobStore.storePreKey(bobPreKey.keyId, bobPreKey);
bobStore.storeSignedPreKey(bobSignedPrekey.keyId, bobSignedPrekey)
const BOB_ADDRESS = new ProtocolAddress('+393278994953', 1)
async function main() {
const aliceSessionCipher = new SessionCipher(aliceStore, BOB_ADDRESS)
const bobSessionCipher = new SessionCipher(bobStore, ALICE_ADDRESS);
// Initiate a session
const aliceToBobSessionBuilder = new SessionBuilder(aliceStore, BOB_ADDRESS)
const bobKeyBundle = generateKeyBundle(bobRegId, bobIdentityKeyPair, bobSignedPrekey, bobPreKey)
aliceToBobSessionBuilder.initOutgoing(bobKeyBundle)
const encryptedMessage1 = await aliceSessionCipher.encrypt(Buffer.from("Hi Bob"))
const decryptedMessage1 = await decryptMessage(bobSessionCipher, encryptedMessage1)
console.log("Bob to Alice [ENCRYPTED]:", encryptedMessage1.body.toString('base64'))
console.log("Alice to Bob:", decryptedMessage1.toString())
const encryptedMessage2 = await bobSessionCipher.encrypt(Buffer.from("Hi Alice"))
const decryptedMessage2 = await decryptMessage(aliceSessionCipher, encryptedMessage2)
console.log("Bob to Alice [ENCRYPTED]:", encryptedMessage2.body.toString('base64'))
console.log("Bob to Alice:", decryptedMessage2.toString())
}
main();