fernet-web
Version:
A library that implements the Fernet algorithm using the Web Crypto API.
61 lines (60 loc) • 2.25 kB
TypeScript
/**
* @author Rich Miles
* @date December 05, 2022
*
* This code provides an implementation of the Fernet symmetric encryption algorithm
* using the Web Crypto API.
*
* @warning This code is provided as is, without any warranty or guarantees. Use at your own risk.
*
* @example
* // Create a new Fernet instance using a secret key
* const fernet = await Fernet.create("-lf4DsgLkOaE1GbtIQKNGU1NPQByMDKP2a6Enl9rclE=");
*
* // Encrypt a message
* const encryptedToken = await fernet.encrypt("Hello world!");
*
* // Decrypt the encrypted message
* const decryptedMessage = await fernet.decrypt(encryptedToken);
*
* // Print the decrypted message
* console.log(decryptedMessage); // Hello world!
*/
/**
* Fernet is a simple and secure way to encrypt and decrypt messages using symmetric encryption.
*/
export default class Fernet {
private signingKey;
private encryptionKey;
/**
* Create a new Fernet instance with the provided signing and encryption keys.
* @param signingKey - A CryptoKey to use for signing messages.
* @param encryptionKey - A CryptoKey to use for encrypting and decrypting messages.
*/
private constructor();
/**
* Generate signing and encryption keys from a secret key.
* @param secretKeyBuffer - A Uint8Array containing the secret key to use.
* @returns A tuple containing the signing and encryption keys.
*/
private static initializeKeys;
/**
* Create a new Fernet instance with a secret key.
* @param secretKey_b64 - A base64-encoded string representation of the secret key to use.
* If null, a new secret key will be generated.
* @returns A new Fernet instance.
*/
static create(secretKey_b64: string | null): Promise<Fernet>;
/**
Encrypt a message into a Fernet token.
@param plainText - The message to encrypt.
@returns A base64-encoded Fernet token.
*/
encrypt(plainText: string): Promise<string>;
/**
* Decrypt a Fernet token and return the plain text message.
* @param token_b64 - A base64-encoded Fernet token.
* @returns The decrypted message as a string.
*/
decrypt(token_b64: string): Promise<string>;
}