lock

[root] / stream.nim

757B

raw
import sodium

type
  KeyPtr = pointer

proc enc*(key: KeyPtr, nonce: array[24, byte], plaintext: pointer, mlen: culonglong, outbuf: pointer): int =
  var clen: culonglong
  let rc = crypto_aead_xchacha20poly1305_ietf_encrypt(outbuf, addr clen, plaintext, mlen,
                                                        nil, 0, nil, addr nonce[0], key)
  if rc == 0:
    return clen.int
  return -1

proc dec*(key: KeyPtr, nonce: array[24, byte], ciphertext: pointer, clen: culonglong, outbuf: pointer): int =
  var mlen: culonglong
  let rc = crypto_aead_xchacha20poly1305_ietf_decrypt(outbuf, addr mlen, nil, ciphertext, clen,
                                                        nil, 0, addr nonce[0], key)
  if rc == 0:
    return mlen.int
  return -1