Skip to content
This repository has been archived by the owner on Sep 13, 2024. It is now read-only.

Commit

Permalink
Moved to latest msrCrypto library and added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kevlened committed Jul 19, 2018
1 parent 25c9ca9 commit 12bccea
Show file tree
Hide file tree
Showing 10 changed files with 12,486 additions and 127 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ crypto.subtle.digest(
* Node 4+
* React Native

For more detailed compatibility details and supported algorithms see [the Compability wiki page](https://github.com/kevlened/isomorphic-webcrypto/wiki/Compatibility).

### React Native

React Native support is implemented using [the Microsoft Research library](https://github.com/kevlened/msrCrypto). The React Native environment only supports `Math.random()`, so [react-native-securerandom](https://github.com/rh389/react-native-securerandom) is used to provide proper entropy. This is handled automatically, except for `crypto.getRandomValues()`, which requires you wait:
Expand Down
59 changes: 59 additions & 0 deletions __tests__/browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
function includes(haystack, needle) {
return haystack.indexOf(needle) > -1;
}

window.Promise = require('bluebird');

var ua = navigator.userAgent;
var isSafari = includes(ua, 'Safari') && !includes(ua, 'Chrome');
var isEdge = includes(ua, 'Edge');
var isIE11 = !!window.MSInputMethodContext && !!document.documentMode;
require('webcrypto-test-suite')({
crypto: require('../src/browser.js'),
shouldSkip: function(spec) {
if (isSafari) {
if (includes(spec,'ES512')) return true;
if (includes(spec,'RS384') && includes(spec,'exportKey')) return true;
if (includes(spec,'RS512') && includes(spec,'exportKey')) return true;
}
if (isEdge) {
// Misidentifies private types as public and has no modulesLength or publicExponent
if (includes(spec,'RS256') && includes(spec,'importKey')) return true;

// Has no modulesLength or publicExponent
if (includes(spec,'RS384') && includes(spec,'importKey')) return true;
if (includes(spec,'RS512') && includes(spec,'importKey')) return true;

// Should return Uint8Array for publicExponent rather than Int8Array
if (includes(spec,'RS256') && includes(spec,'generateKey')) return true;
if (includes(spec,'RS384') && includes(spec,'generateKey')) return true;
if (includes(spec,'RS512') && includes(spec,'generateKey')) return true;

// Could not complete the operation due to error c000000d
if (includes(spec,'RS256') && includes(spec,'sign')) return true;

// Not working at all
if (includes(spec,'ES256')) return true;
if (includes(spec,'ES384')) return true;
if (includes(spec,'ES512')) return true;
}
if (isIE11) {
// All errors are browser events, so there are no error messages to aid debugging

// Misidentifies private types as public
if (includes(spec,'RS256') && includes(spec,'importKey')) return true;

// Missing d, q, dq, p, dp (probably because importKey says it's public)
if (includes(spec,'RS256') && includes(spec,'exportKey')) return true;

// Not working at all
if (includes(spec,'RS256') && includes(spec,'sign')) return true;
if (includes(spec,'RS512') && includes(spec,'sign')) return true;
if (includes(spec,'RS512') && includes(spec,'verify')) return true;
if (includes(spec,'HS512')) return true;
if (includes(spec,'ES256')) return true;
if (includes(spec,'ES384')) return true;
if (includes(spec,'ES512')) return true;
}
}
});
10 changes: 10 additions & 0 deletions __tests__/node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const crypto = require('../src/index');

require('webcrypto-test-suite')({
crypto,
shouldSkip(spec) {
if (spec.includes('ES256') && spec.includes('verify')) return true;
if (spec.includes('ES384') && spec.includes('verify')) return true;
if (spec.includes('ES512')) return true;
}
});
24 changes: 24 additions & 0 deletions __tests__/react-native.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Include additional mocks not included in the react-native preset
global.window = {
Uint8Array,
Promise
};
function XMLHttpRequest() {}
XMLHttpRequest.prototype.getResponseHeader = function() {};
global.XMLHttpRequest = XMLHttpRequest;

delete global.Buffer;

jest.mock('b64-lite', () => require('b64-lite/dist/react-native.js'));
jest.mock('str2buf', () => require('str2buf/dist/str2buf.js'));

const crypto = require('../src/react-native');

require('webcrypto-test-suite')({
crypto,
shouldSkip(spec) {
if (spec.includes('RS256') && spec.includes('generateKey')) return true;
if (spec.includes('RS384') && spec.includes('generateKey')) return true;
if (spec.includes('RS512') && spec.includes('generateKey')) return true;
}
});
3 changes: 3 additions & 0 deletions jest.react-native.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"preset": "react-native"
}
48 changes: 48 additions & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const os = require('os');

let browsers = ['ChromeHeadless', 'FirefoxHeadless'];
if (process.platform === 'darwin') {
browsers.push('Safari');
} else if (process.platform === 'win32' &&
os.release().slice(3) === '10.') {
browsers.push('Edge');
}

module.exports = function(config) {
config.set({
frameworks: ['jasmine'],
files: [
'__tests__/browser.js'
],
preprocessors: {
'__tests__/browser.js': ['webpack']
},
webpack: {
mode: 'none',
node: false
},
reporters: ['progress'],
port: 9876,
colors: true,
// config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,
autoWatch: true,
browsers,
customLaunchers: {
FirefoxHeadless: {
base: 'Firefox',
flags: ['-headless']
},
EdgeVM: {
base: 'VirtualBoxEdge',
keepAlive: true,
uuid: process.env.EDGE_VIRTUAL_BOX_UUID
},
IE11VM: {
base: 'VirtualBoxIE11',
keepAlive: true,
uuid: process.env.IE11_VIRTUAL_BOX_UUID
}
}
})
}
Loading

0 comments on commit 12bccea

Please sign in to comment.