mirror of
https://github.com/logos-messaging/logos-chat.git
synced 2026-02-28 04:43:07 +00:00
36 lines
894 B
Nim
36 lines
894 B
Nim
import proto_types
|
|
|
|
import strformat
|
|
import crypto/ecdh
|
|
import std/[sysrand]
|
|
import results
|
|
import utils
|
|
|
|
export PublicKey, PrivateKey, bytes, createRandomKey, loadPrivateKeyFromBytes, loadPublicKeyFromBytes,
|
|
getPublicKey, Dh, Result, get_addr, `$`
|
|
|
|
|
|
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())
|
|
|
|
proc generate_key*(): PrivateKey =
|
|
createRandomKey().get()
|
|
|
|
|
|
proc toHex*(key: PublicKey): string =
|
|
bytesToHex(key.bytes())
|
|
|
|
proc `$`*(key: PublicKey): string =
|
|
let byteStr = toHex(key)
|
|
fmt"{byteStr[0..3]}..{byteStr[^4 .. ^1]}"
|