-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #111 from Terreii/msrCrypto
Move to msrCrypto
- Loading branch information
Showing
13 changed files
with
9,679 additions
and
364 deletions.
There are no files selected for viewing
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,13 @@ | ||
# EditorConfig from https://EditorConfig.org | ||
|
||
# top-most EditorConfig file | ||
root = true | ||
|
||
# All files | ||
[*] | ||
end_of_line = lf | ||
insert_final_newline = true | ||
charset = utf-8 | ||
indent_style = space | ||
indent_size = 2 | ||
trim_trailing_whitespace = true |
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 |
---|---|---|
@@ -1,29 +1,55 @@ | ||
'use strict' | ||
|
||
var Buffer = require('buffer/').Buffer | ||
var pbkdf2 = require('pbkdf2') | ||
var Promise = require('lie') | ||
var randomBytes = require('randombytes') | ||
var getCrypto = require('./utils/get-web-crypto') | ||
|
||
module.exports = function createKey (password, saltArg) { | ||
var digest = 'sha256' | ||
var subtle = getCrypto().subtle | ||
|
||
var passwordBuffer = toBuffer(password, 'utf-8', 'password') | ||
var digest = 'SHA-256' | ||
var iterations = 100000 | ||
var keyLength = 256 / 8 | ||
|
||
var salt = saltArg != null && typeof saltArg === 'string' && saltArg.length === 32 | ||
? saltArg | ||
: randomBytes(16).toString('hex') | ||
var saltyBuffy = Buffer.from(salt, 'hex') | ||
|
||
return new Promise(function (resolve, reject) { | ||
var saltyBuffy = Buffer.from(salt, 'hex') | ||
|
||
pbkdf2.pbkdf2(password, saltyBuffy, iterations, 256 / 8, digest, function (err, key) { | ||
if (err) { | ||
reject(err) | ||
} else { | ||
resolve({ | ||
key: key, | ||
salt: salt | ||
}) | ||
return subtle.importKey('raw', passwordBuffer, { name: 'PBKDF2' }, false, ['deriveBits']) | ||
|
||
.then(function (key) { | ||
return subtle.deriveBits( | ||
{ | ||
name: 'PBKDF2', | ||
salt: saltyBuffy, | ||
iterations: iterations, | ||
hash: { | ||
name: digest | ||
} | ||
}, | ||
key, | ||
keyLength << 3 | ||
) | ||
}) | ||
|
||
.then(function (res) { | ||
return { | ||
key: Buffer.from(res), | ||
salt: salt | ||
} | ||
}) | ||
}) | ||
} | ||
|
||
function toBuffer (thing, encoding, name) { | ||
if (Buffer.isBuffer(thing)) { | ||
return thing | ||
} else if (typeof thing === 'string') { | ||
return Buffer.from(thing, encoding) | ||
} else if (ArrayBuffer.isView(thing)) { | ||
return Buffer.from(thing.buffer) | ||
} else { | ||
throw new TypeError(name + ' must be a string, a Buffer, a typed array or a DataView') | ||
} | ||
} |
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,45 @@ | ||
// select the web crypto API | ||
|
||
module.exports = getCrypto | ||
|
||
var msrCryptoPolyfill = require('./msrcrypto') | ||
|
||
var globalContext = (function () { | ||
try { | ||
return globalThis // eslint-disable-line no-undef | ||
} catch (err) {} | ||
|
||
try { | ||
return window | ||
} catch (err) {} | ||
|
||
try { | ||
return global | ||
} catch (err) {} | ||
|
||
try { | ||
return process | ||
} catch (err) { | ||
return undefined | ||
} | ||
})() | ||
|
||
/** | ||
* Get the web crypto api | ||
* @returns {Crypto} Web Crypto API | ||
*/ | ||
function getCrypto () { | ||
if (globalContext == null) { | ||
return msrCryptoPolyfill | ||
} | ||
|
||
// globalContext.msCrypto (IE11) isn't listed, because it does not support deriveBits. | ||
// It could be added back, once the CryptoKey object is used. | ||
if (globalContext.crypto && globalContext.crypto.subtle) { | ||
return globalContext.crypto | ||
} | ||
if (globalContext.msrCrypto && globalContext.msrCrypto.subtle) { | ||
return globalContext.msrCrypto | ||
} | ||
return msrCryptoPolyfill | ||
} |
Oops, something went wrong.