436
+ − 1
/*
+ − 2
* The Diffie-Hellman key exchange protocol.
+ − 3
*/
+ − 4
+ − 5
// Our prime number as a base for operations.
+ − 6
var dh_prime = '82818079787776757473727170696867666564636261605958575655545352515049484746454443424140393837363534333231302928272625242322212019181716151413121110987654321';
+ − 7
+ − 8
// g, a primitive root used as an exponent
+ − 9
// (2 and 5 are acceptable, but BigInt is faster with odd numbers)
+ − 10
var dh_g = '5';
+ − 11
+ − 12
/**
+ − 13
* Generates a Diffie-Hellman private key
+ − 14
* @return string(BigInt)
+ − 15
*/
+ − 16
+ − 17
function dh_gen_private()
+ − 18
{
+ − 19
return EnanoMath.RandomInt(256);
+ − 20
}
+ − 21
+ − 22
/**
+ − 23
* Calculates the public key from the private key
+ − 24
* @param string(BigInt)
+ − 25
* @return string(BigInt)
+ − 26
*/
+ − 27
+ − 28
function dh_gen_public(b)
+ − 29
{
+ − 30
return EnanoMath.PowMod(dh_g, b, dh_prime);
+ − 31
}
+ − 32
+ − 33
/**
+ − 34
* Calculates the shared secret.
+ − 35
* @param string(BigInt) Our private key
+ − 36
* @param string(BigInt) Remote party's public key
+ − 37
* @return string(BigInt)
+ − 38
*/
+ − 39
+ − 40
function dh_gen_shared_secret(b, A)
+ − 41
{
+ − 42
return EnanoMath.PowMod(A, b, dh_prime);
+ − 43
}
+ − 44