Python Cryptography.Fernet symmetric encryption, implemented in TypeScript.
  • TypeScript 89.2%
  • CSS 5.3%
  • Python 4.3%
  • HTML 1.2%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2026-03-27 02:57:30 +08:00
.github/workflows ci: fix python environment for compatibility tests 2026-03-27 02:57:30 +08:00
lib refactor: use crypto.subtle.verify replace constantTimeEquals 2026-03-27 02:45:21 +08:00
src initial commit 2026-03-19 03:16:51 +08:00
tests initial commit 2026-03-19 03:16:51 +08:00
.editorconfig initial commit 2026-03-19 03:16:51 +08:00
.gitattributes initial commit 2026-03-19 03:16:51 +08:00
.gitignore initial commit 2026-03-19 03:16:51 +08:00
biome.json initial commit 2026-03-19 03:16:51 +08:00
index.html initial commit 2026-03-19 03:16:51 +08:00
LICENSE initial commit 2026-03-19 03:16:51 +08:00
package.json refactor: use crypto.subtle.verify replace constantTimeEquals 2026-03-27 02:45:21 +08:00
pnpm-lock.yaml initial commit 2026-03-19 03:16:51 +08:00
README.md initial commit 2026-03-19 03:16:51 +08:00
tsconfig.json initial commit 2026-03-19 03:16:51 +08:00
tsconfig.test.json initial commit 2026-03-19 03:16:51 +08:00
vite.config.ts initial commit 2026-03-19 03:16:51 +08:00

Fernet-TS

Python Cryptography.Fernet symmetric encryption, implemented in TypeScript.

Fully compatible with Python's cryptography.fernet module using AES-128-CBC for encryption and HMAC-SHA256 for authentication. Works in both Node.js and browsers, powered by the Web Crypto API.

Install

npm install @gsgfs/fernet

Usage

Quick Start

Decrypt an existing token:

import { Fernet } from "@gsgfs/fernet"

const secret = "DdivR60zj0gvL-6PG1nAj8SwSvyHW0SVxNI3JSvdGLE=";
const token = "gAAAAABpuuo1hxb4p-UnO_KH3OajIgM25-MvtGErt51GHJ-id4r91S-0ChFxxoehwu6SnUzAk4_ulUPHJiIIMkQXen5Rbr3cPIQ4s89WKNCAr6PwYuI2vP0=";

const f = new Fernet(secret);
const decrypted = await f.decrypt(token);        // return Uint8Array
const text = await f.decryptText(token);         // return string
console.log(text);

Encrypt new data:

import { Fernet } from "@gsgfs/fernet"

const f = new Fernet();  // auto-generates secret
const secret = f.getSecret();  // save this for later decryption

const token = await f.encrypt("Hello from Fernet-TS!");
console.log(token);

Working with JSON

Python and TypeScript have different serialization behaviors, which may require attention when exchanging data between them.

// Encrypt any JSON-serializable data
const data = { "Nishiki Asumi": "Yukige Shiki" };
const token = await f.encryptJSON(data);

// Decrypt back to typed object
const restored = await f.decryptJSON<typeof data>(token);

TTL and Token Validation

// Set default TTL (in seconds) in constructor
const f = new Fernet(secret, 3600);  // 1 hour default TTL

// Or specify TTL per-decrypt operation
await f.decrypt(token, 60);  // 60 seconds TTL

// Check token age without decrypting
const age = Fernet.getTokenAge(token);  // seconds
const expired = f.isTokenExpired(token, 3600);

Python Interoperability

Tokens created by this library can be decrypted by Python's cryptography.fernet, and vice versa:

from cryptography.fernet import Fernet

key = Fernet.generate_key()
fernet = Fernet(key)

# Python encrypts
token = fernet.encrypt(b"Hello from Python!")
// TypeScript decrypts (use the same key)
const f = new Fernet(key);
const msg = await f.decryptText(token);

Note: Python's cryptography.fernet requires base64 tokens to include = padding. Since TypeScript's base64url encoding removes trailing = by default, this library automatically adds padding to ensure compatibility.

Use f.encrypt(data, false) to disable automatic padding.