33 lines
845 B
D
33 lines
845 B
D
module angel.utils.cryptography.cryptography;
|
|
|
|
// Internal imports
|
|
import angel.utils.logging;
|
|
import angel.utils.cryptography.curve25519;
|
|
// External imports
|
|
import std.stdio;
|
|
import std.random;
|
|
import std.format;
|
|
|
|
class Cryptography {
|
|
public {
|
|
struct KeyPair {
|
|
ubyte[32] clientSecretKey;
|
|
ubyte[32] sharedSecret;
|
|
}
|
|
}
|
|
|
|
public static KeyPair derive_25519(ubyte[] pk) {
|
|
ubyte[32] sk; // generate client secret key
|
|
for (int i = 0; i < 32; ++i) {
|
|
sk[i] = cast(ubyte)(uniform(0, 256));
|
|
}
|
|
|
|
Logger.log(LogLevel.Debug, "Generated client sk");
|
|
|
|
ubyte[32] ss = curve25519_scalarmult(sk, pk); // derive shared secret out of pk and sk
|
|
|
|
Logger.log(LogLevel.Debug, format("Derived shared secret: %s", ss));
|
|
|
|
return KeyPair(sk, ss);
|
|
}
|
|
} |