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 3877f7b
Show file tree
Hide file tree
Showing 7 changed files with 36 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
7 changes: 6 additions & 1 deletion src/crypto/hash/index.js
Expand Up @@ -75,9 +75,14 @@ if (nodeCrypto) { // Use Node native crypto for all hash functions
ripemd: nodeHash('ripemd160')
};
} else { // Use JS fallbacks
const hasUserAgentCaps = typeof navigator !== 'undefined' && navigator.userAgent;
let webCryptoHash = false;
if (hasUserAgentCaps && navigator.userAgent.indexOf('Edge') === -1) {
webCryptoHash = 'SHA-1';
}
hashFunctions = {
md5: md5,
sha1: asmcryptoHash(Sha1, navigator.userAgent.indexOf('Edge') === -1 && 'SHA-1'),
sha1: asmcryptoHash(Sha1, webCryptoHash),
sha224: hashjsHash(sha224),
sha256: asmcryptoHash(Sha256, 'SHA-256'),
sha384: hashjsHash(sha384, 'SHA-384'),
Expand Down
5 changes: 4 additions & 1 deletion src/crypto/mode/eax.js
Expand Up @@ -48,10 +48,13 @@ async function OMAC(key) {
}

async function CTR(key) {
let
hasUserAgentCaps = typeof navigator !== 'undefined' && navigator.userAgent,
isEdge = hasUserAgentCaps && navigator.userAgent.indexOf('Edge') !== -1;
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
(!isEdge)
) {
key = await webCrypto.importKey('raw', key, { name: 'AES-CTR', length: key.length * 8 }, false, ['encrypt']);
return async function(pt, iv) {
Expand Down
10 changes: 8 additions & 2 deletions src/crypto/mode/gcm.js
Expand Up @@ -52,11 +52,14 @@ async function GCM(cipher, key) {

return {
encrypt: async function(pt, iv, adata = new Uint8Array()) {
const
hasUserAgentCaps = typeof navigator !== 'undefined' && navigator.userAgent,
isEdge = hasUserAgentCaps && navigator.userAgent.indexOf('Edge') !== -1;
if (
!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 && isEdge)
// Edge does not support GCM-en/decrypting without ADATA
) {
return AES_GCM.encrypt(pt, key, iv, adata);
Expand All @@ -66,11 +69,14 @@ async function GCM(cipher, key) {
},

decrypt: async function(ct, iv, adata = new Uint8Array()) {
const
hasUserAgentCaps = typeof navigator !== 'undefined' && navigator.userAgent,
isEdge = hasUserAgentCaps && navigator.userAgent.indexOf('Edge') !== -1;
if (
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 && isEdge)
// 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 3877f7b

Please sign in to comment.