diff --git a/README.md b/README.md index 5d65bed..0b0d716 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/utils/crypto.ts b/src/utils/crypto.ts index 17f01e5..62e5762 100644 --- a/src/utils/crypto.ts +++ b/src/utils/crypto.ts @@ -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) ); } @@ -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; } @@ -31,7 +34,7 @@ export async function generateHMAC( message: Uint8Array, key: CryptoKey ): Promise { - const signature = await window.crypto.subtle.sign('HMAC', key, message); + const signature = await crypto.subtle.sign('HMAC', key, message); return new Uint8Array(signature); } @@ -40,7 +43,7 @@ export async function verifyHMAC( key: CryptoKey, hmac: Uint8Array ): Promise { - let result = await window.crypto.subtle.verify('HMAC', key, hmac, message); + let result = await crypto.subtle.verify('HMAC', key, hmac, message); return result; }