-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace node-rsa with webcrypto api (removes the need for polyfills i…
…n browser) (#101) * downgrade node-rsa * v0.12.5-alpha.0 * upgrade node-rsa again * v0.12.5-alpha.1 * fix: use better assert import * v0.12.5-alpha.2 * change import again * v0.12.5-alpha.3 * comment out asserts for now * v0.12.5-alpha.4 * checkout webcrypto api, get trusted op could not be deciphered error * [worker] wip with webcrypto api * v0.12.5-alpha.5 * [worker] fix local import * v0.12.5-alpha.6 * [worker] fix accessing crypto * v0.12.5-alpha.7 * [worker] correctly print encrypted stuff * v0.12.5-alpha.8 * [worker] add some doc * [worker] better doc for cryptoProvider * [worker/webCryptoRSA] fix logging pubKey * [worker] test all endianness in unit tests * Revert "[worker] test all endianness in unit tests" This reverts commit c8f69f1. * [worker] add swapEndianness function, which doesn't work * Revert "[worker] add swapEndianness function, which doesn't work" This reverts commit 0f56377. * Revert "Revert "[worker] test all endianness in unit tests"" This reverts commit b6ee1c4. * [worker] consistent endianness in key import * [worker] fix: await encryption promise * [worker] working encryptions for a local setup! * [worker] improve efficiency of bit-endianness conversion * [worker] remove tests for byte-endianness as they are unneeded * v0.12.5-alpha.9 * [worker] switch to local-docker network for tests * add webcrypto notes to readme * ignore tests that need a running setup * [worker] remove unnecessary endinanness function args from interface * [worker] cleanup * [worker] export to fix unused warning
- Loading branch information
Showing
31 changed files
with
238 additions
and
151 deletions.
There are no files selected for viewing
Binary file removed
BIN
-42.2 KB
.yarn/cache/@learntheropes-node-rsa-npm-1.1.3-f4fcd76179-d35d8efe2a.zip
Binary file not shown.
Binary file added
BIN
+73.7 KB
.yarn/cache/@peculiar-asn1-schema-npm-2.3.8-cc6ab012ff-1f4dd421f1.zip
Binary file not shown.
Binary file added
BIN
+20.5 KB
.yarn/cache/@peculiar-json-schema-npm-1.1.12-f914d2ea65-b26ececdc2.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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 |
---|---|---|
|
@@ -7,5 +7,5 @@ | |
"publishConfig": { | ||
"directory": "build" | ||
}, | ||
"version": "0.12.4" | ||
"version": "0.12.5-alpha.9" | ||
} |
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
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
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
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
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
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,76 @@ | ||
import BN from "bn.js"; | ||
|
||
/** | ||
* Provides crypto the browser via the native crypto, and in the node-js environment (like our tests) | ||
* via the `@peculiar/webcrypto` polyfill. | ||
*/ | ||
let cryptoProvider: any; | ||
|
||
if (typeof window !== "undefined" && typeof window.crypto !== "undefined") { | ||
cryptoProvider = window.crypto; | ||
} else { | ||
const { Crypto } = require("@peculiar/webcrypto"); | ||
cryptoProvider = new Crypto(); | ||
} | ||
|
||
/** | ||
* Type depending on our environment browser vs. node-js. | ||
*/ | ||
type CryptoKey = import("crypto").KeyObject | import("@peculiar/webcrypto").CryptoKey; | ||
|
||
|
||
export async function parseWebCryptoRSA(data: any): Promise<CryptoKey> { | ||
const keyJson = JSON.parse(data); | ||
|
||
// Convert Base64url-encoded components to ArrayBuffer | ||
const nArrayBuffer = new Uint8Array(new BN(keyJson.n, 'le').toArray()); | ||
const eArrayBuffer = new Uint8Array(new BN(keyJson.e, 'le').toArray()); | ||
|
||
// Import the components into CryptoKey | ||
const publicKey = await cryptoProvider.subtle.importKey( | ||
"jwk", | ||
{ | ||
kty: "RSA", | ||
e: uint8ArrayToBase64Url(eArrayBuffer), | ||
n: uint8ArrayToBase64Url(nArrayBuffer), | ||
ext: true, | ||
}, | ||
{ | ||
name: "RSA-OAEP", | ||
hash: "SHA-256", | ||
}, | ||
true, | ||
["encrypt"] | ||
); | ||
|
||
console.log(`PublicKey: ${JSON.stringify(publicKey)}`); | ||
|
||
return publicKey; | ||
} | ||
|
||
export async function encryptWithPublicKey(data: Uint8Array, publicKey: CryptoKey): Promise<ArrayBuffer> { | ||
const encryptedData = await cryptoProvider.subtle.encrypt( | ||
{ | ||
name: "RSA-OAEP", | ||
}, | ||
publicKey, | ||
data | ||
); | ||
|
||
// console.log(`EncryptedData: ${JSON.stringify({encrypted: buf2hex(encryptedData)})}`); | ||
|
||
return encryptedData; | ||
} | ||
|
||
|
||
function uint8ArrayToBase64Url(uint8Array: Uint8Array): string { | ||
const base64String = btoa(String.fromCharCode(...uint8Array)); | ||
return base64String | ||
.replace(/\+/g, "-") | ||
.replace(/\//g, "_") | ||
.replace(/=+$/, ""); | ||
} | ||
|
||
export function buf2hex(buffer: ArrayBuffer) { // buffer is an ArrayBuffer | ||
return Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join(''); | ||
} |
Oops, something went wrong.