A very light weight package written in Typescript combining node/crypto
and bcrypt
for some helpful methods.
NOTE, I am not using my own crypto with this project, it is using recommend crypto from node and bcrypt.
Import what you need. You will most likely not need to import all of these.
import {
KrBcrypt,
KrHash,
KrHashAlgorithm,
KrRSA,
KrRSAKeyPair
} from "element-ts/krypton";
const password: string = "1234";
const createdPassword: Buffer = await KrBcrypt.createPassword(password);
const checkPassword: boolean = await KrBcrypt.verifyPassword(password, createdPassword);
const hashedData: Buffer = KrHash.hash(Buffer.from("Hello, world!");
const hashedData: Buffer = KrHash.hash(Buffer.from("Hello, world!", KrHashAlgorithm.sha512));
const hasher: KrHash = new KrHash()
hasher.append(Buffer.from("Hello"));
hasher.append(Buffer.from("World"));
const hashedData: Buffer = hasher.hash();
// instance
const cipher: KrCipher = new KrCipher(Buffer.from("password"));
const msg: Buffer = Buffer.from("Hello, world!");
const encryptedData: Buffer = cipher.encrypt(msg);
const decryptedData: Buffer = cipher.decrypt(encryptedData);
console.log(msg === decryptedData);
// static
const encryptedData: Buffer = KrCipher.encrypt(Buffer.from("Hello, world!"), Buffer.from("password"), Buffer.from("the-salt"));
const decryptedData: Buffer = KrCipher.decrypt(encryptedData, Buffer.from("password"), Buffer.from("the-salt"));
const keys: KrRSAKeyPair = KrRSA.generateKeys();
const message: string = "Hello, world!";
const dataUnEncrypted: Buffer = Buffer.from(message);
const dataEncrypted: Buffer = KrRSA.encrypt(dataUnEncrypted, keys.publicKey);
const dataDecrypted: Buffer = KrRSA.decrypt(dataEncrypted, keys.privateKey);
const messageDecrypted: string = dataDecrypted.toString("utf8");
Read about JSON web tokens at jwt.io.
const secret: Buffer = Buffer.from("my-256-bit-secret");
const jwt: string = KrJWT.sign({foo: 1, bar: "Hello, world!"}, secret);
const body: object | undefined = KrJWT.verify(jwt, secret);
You can view the declaration files or even the source code on GitHub.
If you find any bugs please create an issue on GitHub or if you are old fashioned email me at elijah@elijahcobb.com.