Skip to content

Commit

Permalink
chore: update Web Crypto API references to point to Node v19
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelDeBoey committed Aug 22, 2023
1 parent cdd6927 commit e850e23
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 28 deletions.
4 changes: 2 additions & 2 deletions packages/remix-node/sessions/fileStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ export function createFileSessionStorage<Data = SessionData, FlashData = Data>({
let content = JSON.stringify({ data, expires });

while (true) {
// TODO: Once node v16 is available on AWS we should use the webcrypto
// API's crypto.getRandomValues() function here instead.
// TODO: Once Node v19 is supported we should use the globally provided
// // Web Crypto API's crypto.getRandomValues() function here instead.
let randomBytes = crypto.randomBytes(8);
// This storage manages an id space of 2^64 ids, which is far greater
// than the maximum number of files allowed on an NTFS or ext4 volume
Expand Down
50 changes: 24 additions & 26 deletions packages/remix-server-runtime/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,59 +5,57 @@ export type UnsignFunction = (
secret: string
) => Promise<string | false>;

// TODO: Once node v16 is available on AWS we should use the globally provided
// webcrypto "crypto" variable and re-enable this code-path in "./cookies.ts"
// instead of referencing the sign and unsign globals.
// TODO: Once Node v19 is supported we should use the globally provided
// Web Crypto API's and re-enable this code-path in "./cookies.ts"
// instead of referencing the `sign` and `unsign` globals.

// const encoder = new TextEncoder();

// export async function sign(value: string, secret: string): Promise<string> {
// let key = await crypto.subtle.importKey(
// "raw",
// encoder.encode(secret),
// { name: "HMAC", hash: "SHA-256" },
// false,
// ["sign"]
// );

// export const sign = async (value: string, secret: string): Promise<string> => {
// let data = encoder.encode(value);
// let key = await createKey(secret, ["sign"]);
// let signature = await crypto.subtle.sign("HMAC", key, data);
// let hash = btoa(String.fromCharCode(...new Uint8Array(signature))).replace(
// /=+$/,
// ""
// );

// return value + "." + hash;
// }
// };

// export async function unsign(
// export const unsign = async (
// cookie: string,
// secret: string
// ): Promise<string | false> {
// let key = await crypto.subtle.importKey(
// "raw",
// encoder.encode(secret),
// { name: "HMAC", hash: "SHA-256" },
// false,
// ["verify"]
// );

// ): Promise<string | false> => {
// let value = cookie.slice(0, cookie.lastIndexOf("."));
// let hash = cookie.slice(cookie.lastIndexOf(".") + 1);

// let data = encoder.encode(value);
// let key = await createKey(secret, ["verify"]);
// let signature = byteStringToUint8Array(atob(hash));
// let valid = await crypto.subtle.verify("HMAC", key, signature, data);

// return valid ? value : false;
// }
// };

// const createKey = async (
// secret: string,
// usages: CryptoKey["usages"]
// ): Promise<CryptoKey> =>
// crypto.subtle.importKey(
// "raw",
// encoder.encode(secret),
// { name: "HMAC", hash: "SHA-256" },
// false,
// usages
// );

// function byteStringToUint8Array(byteString: string): Uint8Array {
// const byteStringToUint8Array = (byteString: string): Uint8Array => {
// let array = new Uint8Array(byteString.length);

// for (let i = 0; i < byteString.length; i++) {
// array[i] = byteString.charCodeAt(i);
// }

// return array;
// }
// };

0 comments on commit e850e23

Please sign in to comment.