Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Fernet algorithm implementation written in Typescript using Web Crypto API
Fernet algorithm implementation written in Typescript (Web version only, not compatible with Node.js).
This implementation is interoperable with Python version of Fernet. Encrypted text in this Typescript implementation can be decrypted using Python Fernet library and vice versa.
Use the package manager npm to install fernet-ts.
npm i fernet-ts
import { Fernet } from 'fernet-ts';
// Generate secret key
// Returns 32-byte long base64url encoded string
// Example: "Brxd-7fAiRQFYz2eI81ZNzCzJwf7BjAsMjtx-_KH5wo="
const secret = Fernet.generateSecret();
// some random text to encrypt
const message = "This is a top secret message!";
// create new Fernet instance
const f = await Fernet.getInstance(secret);
// encrypt message
// returns base64url encoded string
// Example: "gAAAAABkASMLGab74DqnryApsIrMxLsb5lvRUMnc6YzNYMEt79UVUOBQyqhqGiQu7G9s9h9wnDyWBa78odzUCJvx9jJi5ENrqAL_T6xEgRTmn43mtXOQ42Y="
const token = await f.encrypt(message);
// decrypt token and get message
// returns origin plain text message
// Example: "This is a top secret message!"
const plainText = await f.decrypt(token);
// You can also use static methods for encryption/decryption
const fernetToken = await Fernet.encrypt(message, secret);
const decryptedText = await Fernet.decrypt(fernetToken, secret);
This document describes version 0x80 (currently the only version) of the fernet format.
Conceptually, fernet takes a user-provided message (an arbitrary sequence of bytes), a key (256 bits === 32 bytes), and the current time, and produces a token, which contains the message in a form that can't be read or altered without the key.
To facilitate convenient interoperability, this spec defines the external format of both tokens and keys.
All encryption in this version is done with AES 128 in CBC mode.
All base 64 encoding is done with the "URL and Filename Safe" variant, defined in RFC 4648 as "base64url".
A fernet key is the base64url encoding of the following fields:
Signing-key ‖ Encryption-key
A fernet token is the base64url encoding of the concatenation of the following fields:
Version ‖ Timestamp ‖ IV ‖ Ciphertext ‖ HMAC
Fernet tokens are not self-delimiting. It is assumed that the transport will provide a means of finding the length of each complete fernet token.
This field denotes which version of the format is being used by the token. Currently there is only one version defined, with the value 128 (0x80).
This field is a 64-bit unsigned big-endian integer. It records the number of seconds elapsed between January 1, 1970 UTC and the time the token was created.
The 128-bit Initialization Vector used in AES encryption and decryption of the Ciphertext.
When generating new fernet tokens, the IV must be chosen uniquely for every token. With a high-quality source of entropy, random selection will do this with high probability.
This field has variable size, but is always a multiple of 128 bits, the AES block size. It contains the original input message, padded and encrypted.
This field is the 256-bit SHA256 HMAC, under signing-key, of the concatenation of the following fields:
Version ‖ Timestamp ‖ IV ‖ Ciphertext
Note that the HMAC input is the entire rest of the token verbatim, and that this input is not base64url encoded.
Given a key and message, generate a fernet token with the following steps, in order:
Given a key and token, to verify that the token is valid and recover the original message, perform the following steps, in order:
FAQs
Fernet algorithm implementation written in Typescript using Web Crypto API
The npm package fernet-ts receives a total of 123 weekly downloads. As such, fernet-ts popularity was classified as not popular.
We found that fernet-ts demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.