-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace node-rsa with webcrypto api (removes the need for polyfills in browser) #101
Merged
Merged
Changes from all commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
4cb0373
downgrade node-rsa
clangenb 330b759
v0.12.5-alpha.0
clangenb b0b2d5d
upgrade node-rsa again
clangenb 4882f08
v0.12.5-alpha.1
clangenb 7300c70
fix: use better assert import
clangenb 767ef32
v0.12.5-alpha.2
clangenb d9dc22f
change import again
clangenb 90b0ac8
v0.12.5-alpha.3
clangenb 7ce8b82
comment out asserts for now
clangenb e36887b
v0.12.5-alpha.4
clangenb 6d4981e
checkout webcrypto api, get trusted op could not be deciphered error
clangenb 6790b23
[worker] wip with webcrypto api
clangenb 4bcaacc
v0.12.5-alpha.5
clangenb 6ddea87
[worker] fix local import
clangenb 97f5b46
v0.12.5-alpha.6
clangenb 112d851
[worker] fix accessing crypto
clangenb d845766
v0.12.5-alpha.7
clangenb 0902138
[worker] correctly print encrypted stuff
clangenb 3ec349d
v0.12.5-alpha.8
clangenb 33600bf
[worker] add some doc
clangenb d7a8117
[worker] better doc for cryptoProvider
clangenb e17fafa
[worker/webCryptoRSA] fix logging pubKey
clangenb c8f69f1
[worker] test all endianness in unit tests
clangenb b6ee1c4
Revert "[worker] test all endianness in unit tests"
clangenb 0f56377
[worker] add swapEndianness function, which doesn't work
clangenb 72497e9
Revert "[worker] add swapEndianness function, which doesn't work"
clangenb d1957e6
Revert "Revert "[worker] test all endianness in unit tests""
clangenb a1cf451
[worker] consistent endianness in key import
clangenb 43285d9
[worker] fix: await encryption promise
clangenb e63a9b6
[worker] working encryptions for a local setup!
clangenb d02e158
[worker] improve efficiency of bit-endianness conversion
clangenb 79b5676
[worker] remove tests for byte-endianness as they are unneeded
clangenb dcddc05
v0.12.5-alpha.9
clangenb 1d8049e
[worker] switch to local-docker network for tests
clangenb f7a5ccf
add webcrypto notes to readme
clangenb f1b93ad
ignore tests that need a running setup
clangenb c495bd8
[worker] remove unnecessary endinanness function args from interface
clangenb 39262ad
[worker] cleanup
clangenb 761dc22
[worker] export to fix unused warning
clangenb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a node-js type and it gave me some polyfill errors in the integritee-campaign page