Skip to content

Commit

Permalink
Basic Deno support
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexagon committed Dec 4, 2021
1 parent d6139da commit 037a641
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 8 deletions.
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";
```

#### 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, (typeof navigator === 'undefined' || (typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.indexOf('Edge') === -1)) && 'SHA-1'),
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
(typeof navigator === 'undefined' || (typeof navigator !== 'undefined' && 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 && typeof navigator !== 'undefined' && 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 && typeof navigator !== 'undefined' && navigator.userAgent.indexOf('Edge') !== -1)
// Edge does not support GCM-en/decrypting without ADATA
) {
return AES_GCM.decrypt(ct, key, iv, adata);
Expand Down
7 changes: 5 additions & 2 deletions src/util.js
Expand Up @@ -429,8 +429,11 @@ const util = {
const os = require('os');
return os.cpus().length;
}

return navigator.hardwareConcurrency || 1;
if (typeof navigator !== 'undefined' && navigator.hardwareConcurrency) {
return navigator.hardwareConcurrency || 1;
} else {
return 1;
}
},

isEmailAddress: function(data) {
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;
}
}
}
});
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)) {
this.skip(); // Old Edge doesn't support crypto.getRandomValues inside a Worker.
}
try {
Expand Down

0 comments on commit 037a641

Please sign in to comment.