2025-07-05 14:54:19 -07:00
|
|
|
import proto_types
|
|
|
|
|
|
2025-07-16 16:17:22 -07:00
|
|
|
import crypto/ecdh
|
2025-07-05 14:54:19 -07:00
|
|
|
import std/[sysrand]
|
2025-07-16 16:17:22 -07:00
|
|
|
import results
|
2025-07-05 14:54:19 -07:00
|
|
|
|
2025-07-16 16:17:22 -07:00
|
|
|
export PublicKey, PrivateKey, bytes, createRandomKey, loadPrivateKeyFromBytes, loadPublicKeyFromBytes,
|
|
|
|
|
getPublicKey, Dh, Result
|
2025-07-05 14:54:19 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
proc encrypt_plain*[T: EncryptableTypes](frame: T): EncryptedPayload =
|
|
|
|
|
return EncryptedPayload(
|
|
|
|
|
plaintext: Plaintext(payload: encode(frame)),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
proc decrypt_plain*[T: EncryptableTypes](ciphertext: Plaintext, t: typedesc[
|
|
|
|
|
T]): Result[T, string] =
|
|
|
|
|
|
|
|
|
|
let obj = decode(ciphertext.payload, T)
|
|
|
|
|
if obj.isErr:
|
|
|
|
|
return err("Protobuf decode failed: " & obj.error)
|
|
|
|
|
result = ok(obj.get())
|
|
|
|
|
|
2025-07-16 16:17:22 -07:00
|
|
|
proc generate_key*(): PrivateKey =
|
|
|
|
|
createRandomKey().get()
|
2025-07-05 14:54:19 -07:00
|
|
|
|