Skip to content
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

Basic Deno support #1448

Merged
merged 3 commits into from Dec 7, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Expand Up @@ -10,6 +10,7 @@ OpenPGP.js [![BrowserStack Status](https://automate.browserstack.com/badge.svg?b
- [Performance](#performance)
- [Getting started](#getting-started)
- [Node.js](#nodejs)
- [Deno (experimental)](#deno-experimental)
- [Browser (webpack)](#browser-webpack)
- [Browser (plain files)](#browser-plain-files)
- [Examples](#examples)
Expand Down Expand Up @@ -109,6 +110,14 @@ Or as an ES6 module, from an .mjs file:
import * as openpgp from 'openpgp';
```

#### Deno (experimental)

Import as an ES6 module, from an .mjs file on a CDN of your choice.

```js
import * as openpgp from "https://cdn.jsdelivr.net/npm/openpgp@5/dist/openpgp.mjs";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a security-related module like OpenPGP.js, I would prefer not to suggest to use a CDN, at least without something like Subresource Integrity (don't know if it's possible to add it here?)

Copy link
Contributor Author

@Hexagon Hexagon Dec 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe deno.land/x can be seen as a more trusted source? This will require a maintaner to register the module at https://deno.land/x, and add a webhook to github.

For now, maybe we can keep it anonymous, with an example showing a local import?

Edit: Suggested a more anonymous approach in latest commit, see new single comment.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alrighty, yeah. We can do that after merging this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sweet 👍

```

#### Browser (webpack)

Install OpenPGP.js using npm and save it in your devDependencies:
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/hash/index.js
Expand Up @@ -77,7 +77,7 @@ if (nodeCrypto) { // Use Node native crypto for all hash functions
} else { // Use JS fallbacks
hashFunctions = {
md5: md5,
sha1: asmcryptoHash(Sha1, navigator.userAgent.indexOf('Edge') === -1 && 'SHA-1'),
sha1: asmcryptoHash(Sha1, (!navigator.userAgent || (navigator.userAgent && navigator.userAgent.indexOf('Edge') === -1)) && 'SHA-1'),
Hexagon marked this conversation as resolved.
Show resolved Hide resolved
sha224: hashjsHash(sha224),
sha256: asmcryptoHash(Sha256, 'SHA-256'),
sha384: hashjsHash(sha384, 'SHA-384'),
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/mode/eax.js
Expand Up @@ -51,7 +51,7 @@ async function CTR(key) {
if (
util.getWebCrypto() &&
key.length !== 24 && // WebCrypto (no 192 bit support) see: https://www.chromium.org/blink/webcrypto#TOC-AES-support
navigator.userAgent.indexOf('Edge') === -1
(!navigator.userAgent || (navigator.userAgent && navigator.userAgent.indexOf('Edge') === -1))
) {
key = await webCrypto.importKey('raw', key, { name: 'AES-CTR', length: key.length * 8 }, false, ['encrypt']);
return async function(pt, iv) {
Expand Down
4 changes: 2 additions & 2 deletions src/crypto/mode/gcm.js
Expand Up @@ -56,7 +56,7 @@ async function GCM(cipher, key) {
!pt.length ||
// iOS does not support GCM-en/decrypting empty messages
// Also, synchronous en/decryption might be faster in this case.
(!adata.length && navigator.userAgent.indexOf('Edge') !== -1)
(!adata.length && navigator.userAgent && navigator.userAgent.indexOf('Edge') !== -1)
// Edge does not support GCM-en/decrypting without ADATA
) {
return AES_GCM.encrypt(pt, key, iv, adata);
Expand All @@ -70,7 +70,7 @@ async function GCM(cipher, key) {
ct.length === tagLength ||
// iOS does not support GCM-en/decrypting empty messages
// Also, synchronous en/decryption might be faster in this case.
(!adata.length && navigator.userAgent.indexOf('Edge') !== -1)
(!adata.length && navigator.userAgent && navigator.userAgent.indexOf('Edge') !== -1)
// Edge does not support GCM-en/decrypting without ADATA
) {
return AES_GCM.decrypt(ct, key, iv, adata);
Expand Down
1 change: 0 additions & 1 deletion src/util.js
Expand Up @@ -429,7 +429,6 @@ const util = {
const os = require('os');
return os.cpus().length;
}

Hexagon marked this conversation as resolved.
Show resolved Hide resolved
return navigator.hardwareConcurrency || 1;
},

Expand Down
4 changes: 3 additions & 1 deletion test/general/streaming.js
Expand Up @@ -918,7 +918,9 @@ function tests() {
if (util.detectNode()) {
coresStub.restore();
} else {
delete navigator.hardwareConcurrency;
if (navigator && navigator.hardwareConcurrency !== void 0) {
delete navigator.hardwareConcurrency;
}
Hexagon marked this conversation as resolved.
Show resolved Hide resolved
}
}
});
Expand Down
2 changes: 1 addition & 1 deletion test/worker/application_worker.js
Expand Up @@ -12,7 +12,7 @@ module.exports = () => tryTests('Application Worker', tests, {
function tests() {

it('Should support loading OpenPGP.js from inside a Web Worker', async function() {
if (/Edge/.test(navigator.userAgent)) {
if (navigator && navigator.userAgent && /Edge/.test(navigator.userAgent)) {
Hexagon marked this conversation as resolved.
Show resolved Hide resolved
this.skip(); // Old Edge doesn't support crypto.getRandomValues inside a Worker.
}
try {
Expand Down