abcrypt_wasm/decrypt.rs
1// SPDX-FileCopyrightText: 2022 Shun Sakai
2//
3// SPDX-License-Identifier: Apache-2.0 OR MIT
4
5//! Decrypts from the abcrypt encrypted data format.
6
7use wasm_bindgen::{JsError, prelude::wasm_bindgen};
8
9/// Decrypts `ciphertext` and into a newly allocated `Uint8Array`.
10///
11/// # Errors
12///
13/// Returns an error if any of the following are true:
14///
15/// - `ciphertext` is shorter than 164 bytes.
16/// - The magic number is invalid.
17/// - The version number is the unsupported abcrypt version number.
18/// - The version number is the unrecognized abcrypt version number.
19/// - The Argon2 type is invalid.
20/// - The Argon2 version is invalid.
21/// - The Argon2 parameters are invalid.
22/// - The Argon2 context is invalid.
23/// - The MAC (authentication tag) of the header is invalid.
24/// - The MAC (authentication tag) of the ciphertext is invalid.
25#[wasm_bindgen]
26pub fn decrypt(ciphertext: &[u8], passphrase: &[u8]) -> Result<Vec<u8>, JsError> {
27 abcrypt::decrypt(ciphertext, passphrase).map_err(JsError::from)
28}