-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove
require('crypto')
imports in browser client (#721)
- Loading branch information
Showing
14 changed files
with
444 additions
and
384 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { crypto } from "https://deno.land/[email protected]/crypto/mod.ts"; | ||
|
||
import type { CryptoUtils } from "./utils.ts"; | ||
|
||
const cryptoUtils: CryptoUtils = { | ||
async randomBytes(size: number): Promise<Uint8Array> { | ||
const buf = new Uint8Array(size); | ||
return crypto.getRandomValues(buf); | ||
}, | ||
|
||
async H(msg: Uint8Array): Promise<Uint8Array> { | ||
return new Uint8Array(await crypto.subtle.digest("SHA-256", msg)); | ||
}, | ||
|
||
async HMAC(key: Uint8Array, msg: Uint8Array): Promise<Uint8Array> { | ||
return new Uint8Array( | ||
await crypto.subtle.sign( | ||
"HMAC", | ||
await crypto.subtle.importKey( | ||
"raw", | ||
key, | ||
{ | ||
name: "HMAC", | ||
hash: { name: "SHA-256" }, | ||
}, | ||
false, | ||
["sign"] | ||
), | ||
msg | ||
) | ||
); | ||
}, | ||
}; | ||
|
||
export default cryptoUtils; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import type { CryptoUtils } from "./utils"; | ||
|
||
let cryptoUtils: CryptoUtils; | ||
|
||
if (typeof crypto === "undefined") { | ||
// tslint:disable-next-line:no-var-requires | ||
const nodeCrypto = require("crypto"); | ||
|
||
cryptoUtils = { | ||
randomBytes(size: number): Promise<Uint8Array> { | ||
return new Promise((resolve, reject) => { | ||
nodeCrypto.randomBytes(size, (err: Error | null, buf: Buffer) => { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
resolve(buf); | ||
} | ||
}); | ||
}); | ||
}, | ||
|
||
async H(msg: Uint8Array): Promise<Uint8Array> { | ||
const sign = nodeCrypto.createHash("sha256"); | ||
sign.update(msg); | ||
return sign.digest(); | ||
}, | ||
|
||
async HMAC(key: Uint8Array, msg: Uint8Array): Promise<Uint8Array> { | ||
const hm = nodeCrypto.createHmac("sha256", key); | ||
hm.update(msg); | ||
return hm.digest(); | ||
}, | ||
}; | ||
} else { | ||
// tslint:disable-next-line:no-var-requires | ||
cryptoUtils = require("./browserCrypto").default; | ||
} | ||
|
||
export default cryptoUtils; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,3 @@ | ||
import { crypto } from "https://deno.land/[email protected]/crypto/mod.ts"; | ||
|
||
export async function randomBytes(size: number): Promise<Uint8Array> { | ||
const buf = new Uint8Array(size); | ||
return crypto.getRandomValues(buf); | ||
} | ||
|
||
export async function H(msg: Uint8Array): Promise<Uint8Array> { | ||
return new Uint8Array(await crypto.subtle.digest("SHA-256", msg)); | ||
} | ||
|
||
export async function HMAC( | ||
key: Uint8Array, | ||
msg: Uint8Array | ||
): Promise<Uint8Array> { | ||
return new Uint8Array( | ||
await crypto.subtle.sign( | ||
"HMAC", | ||
await crypto.subtle.importKey( | ||
"raw", | ||
key, | ||
{ | ||
name: "HMAC", | ||
hash: { name: "SHA-256" }, | ||
}, | ||
false, | ||
["sign"] | ||
), | ||
msg | ||
) | ||
); | ||
} | ||
|
||
export function getEnv(envName: string, required = false): string | undefined { | ||
if (!required) { | ||
const state = Deno.permissions.querySync({ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import type { CryptoUtils } from "./utils"; | ||
|
||
const cryptoUtils: CryptoUtils = { | ||
async randomBytes(size: number): Promise<Uint8Array> { | ||
return crypto.getRandomValues(new Uint8Array(size)); | ||
}, | ||
|
||
async H(msg: Uint8Array): Promise<Uint8Array> { | ||
return new Uint8Array(await crypto.subtle.digest("SHA-256", msg)); | ||
}, | ||
|
||
async HMAC(key: Uint8Array, msg: Uint8Array): Promise<Uint8Array> { | ||
return new Uint8Array( | ||
await crypto.subtle.sign( | ||
"HMAC", | ||
await crypto.subtle.importKey( | ||
"raw", | ||
key, | ||
{ | ||
name: "HMAC", | ||
hash: { name: "SHA-256" }, | ||
}, | ||
false, | ||
["sign"] | ||
), | ||
msg | ||
) | ||
); | ||
}, | ||
}; | ||
|
||
export default cryptoUtils; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.