1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
use crate::{
    class::{
        crypto::{Cipher, Decipher, DiffieHellman, DiffieHellmanGroup, Ecdh, Hash, Hmac, KeyObject, Sign, Verify},
        Buffer,
    },
    interface::{
        CryptoConstants,
        GenerateKeyPairOptions,
        GenerateKeyPairSyncReturn,
        ScryptOptions,
        StreamTransformOptions,
        StreamWritableOptions,
    },
};
use js_sys::Function;
use wasm_bindgen::prelude::*;

#[wasm_bindgen(module = "crypto")]
extern {
    //*******************//
    // Module Properties //
    //*******************//

    pub static constants: CryptoConstants;

    //****************//
    // Module Methods //
    //****************//

    #[wasm_bindgen(js_name = "createCipheriv")]
    pub fn create_cipheriv(
        algorithm: &str,
        key: &JsValue,
        iv: &JsValue,
        options: Option<StreamTransformOptions>,
    ) -> Cipher;

    #[wasm_bindgen(js_name = "createDecipheriv")]
    pub fn create_decipheriv(
        algorithm: &str,
        key: &JsValue,
        iv: &JsValue,
        options: Option<StreamTransformOptions>,
    ) -> Decipher;

    #[wasm_bindgen(js_name = "createDiffieHellman")]
    pub fn create_diffie_hellman_with_prime(
        prime: &JsValue,
        prime_encoding: Option<&str>,
        generator: &JsValue,
        generator_encoding: Option<&str>,
    ) -> DiffieHellman;

    #[wasm_bindgen(js_name = "createDiffieHellman")]
    pub fn create_diffie_hellman(prime_length: u32, generator: &JsValue) -> DiffieHellman;

    #[wasm_bindgen(js_name = "createECDH")]
    pub fn create_ecdh(curve_name: &str) -> Ecdh;

    #[wasm_bindgen(js_name = "createHash")]
    pub fn create_hash(algorithm: &str, options: Option<StreamTransformOptions>) -> Hash;

    #[wasm_bindgen(js_name = "createHmac")]
    pub fn create_hmac(algorithm: &str, key: &JsValue, option: Option<StreamTransformOptions>) -> Hmac;

    #[wasm_bindgen(js_name = "createPrivateKey")]
    pub fn create_private_key(key: &JsValue) -> KeyObject;

    #[wasm_bindgen(js_name = "createPublicKey")]
    pub fn create_public_key(key: &JsValue) -> KeyObject;

    #[wasm_bindgen(js_name = "createSecretKey")]
    pub fn create_secret_key(key: &Buffer) -> KeyObject;

    #[wasm_bindgen(js_name = "createSign")]
    pub fn create_sign(algorithm: &str, options: Option<StreamWritableOptions>) -> Sign;

    #[wasm_bindgen(js_name = "createVerify")]
    pub fn create_verify(algorithm: &str, options: Option<StreamWritableOptions>) -> Verify;

    #[wasm_bindgen(js_name = "generateKeyPair")]
    pub fn generate_key_pair(kind: &str, options: &GenerateKeyPairOptions, callback: &Function);

    #[wasm_bindgen(js_name = "generateKeyPairSync")]
    pub fn generate_key_pair_sync(kind: &str, options: &GenerateKeyPairOptions) -> GenerateKeyPairSyncReturn;

    #[wasm_bindgen(js_name = "getCiphers")]
    pub fn get_ciphers() -> Box<[JsValue]>;

    #[wasm_bindgen(js_name = "getCurves")]
    pub fn get_curves() -> Box<[JsValue]>;

    #[wasm_bindgen(js_name = "getDiffieHellman")]
    pub fn get_diffie_hellman(group_name: &str) -> DiffieHellmanGroup;

    #[wasm_bindgen(js_name = "getFips")]
    pub fn get_fips(group_name: &str) -> DiffieHellmanGroup;

    #[wasm_bindgen(js_name = "getHashes")]
    pub fn get_hashes() -> Box<[JsValue]>;

    #[wasm_bindgen(js_name = "pbkdf2")]
    pub fn pbkdf2(password: &JsValue, salt: &JsValue, iterations: u32, keylen: u32, digest: &str, callback: &Function);

    #[wasm_bindgen(js_name = "pbkdf2Sync")]
    pub fn pbkdf2_sync(password: &JsValue, salt: &JsValue, iterations: u32, keylen: u32, digest: &str) -> Buffer;

    #[wasm_bindgen(js_name = "privateDecrypt")]
    pub fn private_decrypt(private_key: &JsValue, buffer: &JsValue) -> Buffer;

    #[wasm_bindgen(js_name = "privateEncrypt")]
    pub fn private_encrypt(private_key: &JsValue, buffer: &JsValue) -> Buffer;

    #[wasm_bindgen(js_name = "publicDecrypt")]
    pub fn public_decrypt(key: &JsValue, buffer: &JsValue) -> Buffer;

    #[wasm_bindgen(js_name = "publicEncrypt")]
    pub fn public_encrypt(key: &JsValue, buffer: &JsValue) -> Buffer;

    #[wasm_bindgen(js_name = "randomBytes")]
    pub fn random_bytes(size: f64, callback: Option<&Function>) -> Buffer;

    #[wasm_bindgen(js_name = "randomFillSync")]
    pub fn random_fill_sync(buffer: &JsValue, offset: Option<f64>, size: Option<f64>) -> JsValue;

    #[wasm_bindgen(js_name = "randomFill")]
    pub fn random_fill(buffer: &JsValue, offset: Option<f64>, size: Option<f64>, callback: &Function);

    pub fn scrypt(password: &JsValue, salt: &JsValue, keylen: f64, options: Option<ScryptOptions>, callback: &Function);

    #[wasm_bindgen(js_name = "scryptSync")]
    pub fn scrypt_sync(password: &JsValue, salt: &JsValue, keylen: f64, options: Option<ScryptOptions>) -> Buffer;

    #[wasm_bindgen(js_name = "setEngine")]
    pub fn set_engine(engine: &str, flags: Option<i32>);

    #[wasm_bindgen(js_name = "setFips")]
    pub fn set_fips(enable: bool);

    pub fn sign(algorithm: &JsValue, data: &JsValue, key: &JsValue) -> Buffer;

    #[wasm_bindgen(js_name = "timingSafeEqual")]
    pub fn timing_safe_equal(a: &JsValue, b: &JsValue) -> bool;

    pub fn verify(algorithm: &JsValue, data: &JsValue, key: &JsValue, signature: &JsValue) -> bool;
}