Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# fernet-ts

Fernet algorithm implementation written in Typescript (Web version only, not compatible with Node.js).
Fernet algorithm implementation written in Typescript (Compatible with browsers, Deno 1.11+, Node.js 19+ and maybe other runtimes).

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.
Expand Down
13 changes: 8 additions & 5 deletions src/utils/crypto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export function fromBase64Url(base64url: string): Uint8Array {
return Uint8Array.from(
window.atob(base64url.replace(/_/g, '/').replace(/-/g, '+')),
globalThis.atob(base64url.replace(/_/g, '/').replace(/-/g, '+')),
(c) => c.charCodeAt(0)
);
}
Expand All @@ -10,12 +10,15 @@ export function toBase64Url(bytes: Uint8Array): string {
bytes.forEach((byte) => {
chars.push(String.fromCharCode(byte));
});
return window.btoa(chars.join('')).replace(/\+/g, '-').replace(/\//g, '_');
return globalThis
.btoa(chars.join(''))
.replace(/\+/g, '-')
.replace(/\//g, '_');
}

export function getRandomBytes(length: number): Uint8Array {
const randomBytes = new Uint8Array(length);
window.crypto.getRandomValues(randomBytes);
crypto.getRandomValues(randomBytes);
return randomBytes;
}

Expand All @@ -31,7 +34,7 @@ export async function generateHMAC(
message: Uint8Array,
key: CryptoKey
): Promise<Uint8Array> {
const signature = await window.crypto.subtle.sign('HMAC', key, message);
const signature = await crypto.subtle.sign('HMAC', key, message);
return new Uint8Array(signature);
}

Expand All @@ -40,7 +43,7 @@ export async function verifyHMAC(
key: CryptoKey,
hmac: Uint8Array
): Promise<boolean> {
let result = await window.crypto.subtle.verify('HMAC', key, hmac, message);
let result = await crypto.subtle.verify('HMAC', key, hmac, message);
return result;
}

Expand Down