-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
44 lines (35 loc) · 1.46 KB
/
index.js
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
"use strict";
// Dependencies
const crypto = require("crypto")
// Main
class DiffieDiffie {
load(){
return new Promise(async(resolve)=>{
this.server = crypto.createDiffieHellman(1000)
this.serverPrime = this.server.getPrime()
this.serverClient = crypto.createDiffieHellman(this.serverPrime)
this.serverClient.generateKeys()
this.serverClient2 = crypto.createDiffieHellman(this.serverPrime)
this.serverClient2.generateKeys()
resolve()
})
}
getPublicKey(){
return `${this.serverClient.getPublicKey().toString("hex")}.${this.serverPrime.toString("hex")}.${this.serverClient2.getPublicKey().toString("hex")}`
}
ServerGetSecret(cPublicKey){
return new Promise(async(resolve)=>{
const key1 = this.serverClient.computeSecret(Buffer.from(cPublicKey, "hex"))
const key2 = this.serverClient2.computeSecret(Buffer.from(cPublicKey, "hex"))
resolve(Buffer.from(key1.toString("ucs-2") + key2.toString("ucs-2")))
})
}
clientGetSecret(client, publicKey){
return new Promise(async(resolve)=>{
const key1 = client.computeSecret(Buffer.from(publicKey.split(".")[0], "hex"))
const key2 = client.computeSecret(Buffer.from(publicKey.split(".")[2], "hex"))
resolve(Buffer.from(key1.toString("ucs-2") + key2.toString("ucs-2")))
})
}
}
module.exports = DiffieDiffie