Skip to content

Examples

MustafaHi edited this page Aug 16, 2021 · 2 revisions

Include the script file:

Botan.mjs || Botan.tis

The binding library API offers:

  1. Hashing (SHA-256, MD5...)
  2. Password Hashing (Argon2, Bcrypt)
  3. Encoding & Decoding (Base64, Hex)
  4. Encrypting & Decrypting (AES, Twofish...)
  5. Other Utilities

! The API is very data sensitive, invalid data may lead to crash


Hash

Hash a string using given method https://botan.randombit.net/handbook/api_ref/hash.html

Code:

Botan.hash("SHA-256", "data")

Return: "3A6EB0790F39AC87C94F3856B2DD2C5D110E6811602261A9A923D3BB23ADC8B7"


Password

Generate password hashing using methods [argon, bcrypt] https://botan.randombit.net/handbook/api_ref/passhash.html

Generate Methods : argon, bcrypt

await Botan.password("bcrypt", "data")

Return: "$2a$12$dlkARcTc2dBCQjWb5tXIYOi0TtjXEzcFbPUDL.CqmOkpOmL3KXp5e"

! Each time you call this code even with the same data, the returned value will be different, Therefore you need a checker function

Check Methods : check-argon, check-bcrypt

await Botan.password("check-bcrypt", "data", "$2a$12$dlkARcTc2dBCQjWb5tXIYOi0TtjXEzcFbPUDL.CqmOkpOmL3KXp5e")

Return: true


Codec

Methods : base64, hex

Encode

Botan.encode("base64", "data");

Return: "ZGF0YQ=="

Decode

Botan.decode("base64", "ZGF0YQ==");

Return: "data"


Cipher

Encrypt

  1. Key must be of specified length per method requirement, you should hash it.
  2. If IV(nonce) is provided it will be used to encrypt the data, otherwise a new one is generated.
var crypto = await Botan.cipher("AES-256/CBC", "data", Botan.hash("SHA-256", "key"));

    crypto.data
    // Return: "6B3EBE861F24420F7FD5DCC4CF7CB0F3"
    crypto.iv
    // Return: "C5B8CF8DFC4647318A65271DFE39CF52"
    // randomly generated, you can provide an IV as 4th argument.

Return: Object with data and iv

Decrypt

  1. Key must be of specified length per method requirement, you should hash it.
  2. IV(nonce) must be provided
// The same values returned from the previous sample now will be decrypted.
var crypto = await Botan.decipher("AES-256/CBC", "6B3EBE861F24420F7FD5DCC4CF7CB0F3",
                   Botan.hash("SHA-256", "key"), "C5B8CF8DFC4647318A65271DFE39CF52");

    crypto.data
    // Return: "data"
    crypto.iv
    // Return: "C5B8CF8DFC4647318A65271DFE39CF52"

Return: Object with data and iv


Utilities

IV(nonce)

method : https://botan.randombit.net/handbook/api_ref/block_cipher.html

Botan.iv("AES-256/CBC")

Return: "C3C2DCFF84452487DF929C5DB3E66FDF" - randomly generated per method requirement.

Clone this wiki locally