2023-11-21 12:38:43 -04:00
|
|
|
import { ChaCha20Poly1305 } from "@stablelib/chacha20poly1305";
|
|
|
|
|
|
|
|
|
|
import { bytes32 } from "./@types/basic.js";
|
|
|
|
|
import { Cipher } from "./crypto.js";
|
2023-11-21 13:19:09 -04:00
|
|
|
import { Nonce } from "./nonce.js";
|
2023-11-21 12:38:43 -04:00
|
|
|
|
|
|
|
|
export class ChaChaPoly implements Cipher {
|
2023-11-21 13:19:09 -04:00
|
|
|
encrypt(k: bytes32, nonce: Nonce, ad: Uint8Array, plaintext: Uint8Array): Uint8Array {
|
2023-11-21 12:38:43 -04:00
|
|
|
const ctx = new ChaCha20Poly1305(k);
|
2023-11-21 13:19:09 -04:00
|
|
|
const n = nonce.getBytes(true);
|
2023-11-21 12:38:43 -04:00
|
|
|
return ctx.seal(n, plaintext, ad);
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-21 13:19:09 -04:00
|
|
|
decrypt(k: bytes32, nonce: Nonce, ad: Uint8Array, ciphertext: Uint8Array): Uint8Array | null {
|
2023-11-21 12:38:43 -04:00
|
|
|
const ctx = new ChaCha20Poly1305(k);
|
2023-11-21 13:19:09 -04:00
|
|
|
const n = nonce.getBytes(true);
|
2023-11-21 12:38:43 -04:00
|
|
|
return ctx.open(n, ciphertext, ad);
|
|
|
|
|
}
|
|
|
|
|
}
|