God Crypto
A pure Javascript/Typescript cryptography implementation for Deno. We will try to use WebCrypto if available, then fallback to WebAssembly implementation if available, otherwise, we will use pure Javascript implementation.
WebCrypto | WebAssembly | Javascript | |
---|---|---|---|
AES | |||
AES-CBC |
✔️ | ❌ | ✔️ |
AES-CFB |
❌ | ❌ | ✔️ |
AES-ECB |
❌ | ❌ | ✔️ |
RSA | |||
RSA-PKCS1 v1.5 |
❌ | ❌ | ✔️ |
RSA-OAEP |
✔️ | ❌ | ✔️ |
More algorithm supports is one the way
AES
Example
import { AES } from "https://deno.land/x/god_crypto@v.1.1.0/mod.ts";
const aes = new AES("Hello World AES!", {
mode: "cbc",
iv: "random 16byte iv",
});
const cipher = await aes.encrypt("This is AES-128-CBC. It works.");
console.log(cipher.hex());
// 41393374609eaee39fbe57c96b43a9da0d547c290501be50f983ecaac6c5fd1c
const plain = await aes.decrypt(ciper);
console.log(plain.toString());
// This is AES-128-CBC. It works.
Syntax
new AES(key, {
mode: "cbc" | "ebc", // default cbc
iv: string | UInt8Array, // default [0, 0, ...., 0]
padding: "pkcs5", // default pkcs5
});
RSA
import { RSA } from "https://deno.land/x/god_crypto@v.1.1.0/mod.ts";
const publicKey = RSA.parseKey(Deno.readTextFileSync("./public.pem"));
const cipher = await new RSA(publicKey).encrypt("Hello World");
console.log(ciper.base64());
const privateKey = RSA.parseKey(Deno.readTextFileSync("./private.pem"));
const plain = await new RSA(privateKey).decrypt(cipher);
console.log(plain.toString());
// More examples:
new RSA(publicKey);
new RSA(publicKey, { padding: "oaep", hash: "sha256" });
new RSA(publicKey, { padding: "pkcs1" });
Other Utility
We also provide encoding utility.
import { encode } from "https://deno.land/x/god_crypto@v.1.1.0/mod.ts";
// Converting hex to string
encode.hex("676f645f63727970746f20726f636b7321").toString(); // "god_crypto rocks!"
// Converting hex to base64
encode.hex("676f645f63727970746f20726f636b7321").base64(); // Z29kX2NyeXB0byByb2NrcyE=
// Converting base64 to hex
encode.base64("Z29kX2NyeXB0byByb2NrcyE=").hex(); // 676f645f63727970746f20726f636b7321
// Convert hex/base64 to Uint8Array
encode.base64("Z29kX2NyeXB0byByb2NrcyE="); // Uint8Array object
encode.hex("676f645f63727970746f20726f636b7321"); // Uint8Array object