//*******************************************************************************
//
// Copyright 2014 Microsoft
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//*******************************************************************************
var publicMethods = {
/// Microsoft Research Javascript Crypto Library Subtle interface.
subtle: msrcryptoSubtle,
getRandomValues: function(array) {
///
/// Places cryptographically random values into the given array.
///
///
///
///
/// Places cryptographically random values into the given array.
///
/// Returns ArrayBufferView if supported.
///
var i;
var randomValues = msrcryptoPseudoRandom.getBytes(array.length);
for (i = 0; i < array.length; i+=1) {
array[i] = randomValues[i];
}
return array;
},
initPrng: function (entropyData) {
///
/// Add entropy to the PRNG.
/// Entropy input to seed or reseed the PRNG.
///
var entropyDataType = Object.prototype.toString.call(entropyData);
if (entropyDataType !== "[object Array]" && entropyDataType !== "[object Uint8Array]") {
throw new Error("entropyData must be a Array or Uint8Array");
}
// Mix the user-provided entropy into the entropy pool - only in the main thread.
entropyPool && entropyPool.reseed(entropyData);
// Reseed the PRNG that was initialized below
msrcryptoPseudoRandom.reseed(entropyPool.read(48));
fprngEntropyProvided = true;
},
toBase64: function (data, toBase64Url) {
///
/// Convert string or array data to Base64.
/// Byte values (numbers 0-255)
/// Return Base64Url encoding (this is different from Base64 encoding.)
///
///
///
/// Convert string or array data to Base64.
///
/// Return Base64Url encoding (this is different from Base64 encoding.)
///
///
return msrcryptoUtilities.toBase64(data, false);
},
base64ToString: function (base64String) {
///
/// Decode a Base64 encoded string to a plain string.
/// Base64 encoded string.
///
///
return msrcryptoUtilities.base64ToString(base64String);
},
/// URL of the this msrCrypto script.
url : scriptUrl
};
// Expose the math library if present
if (typeof cryptoMath !== "undefined") {
publicMethods.cryptoMath = cryptoMath;
}
if (typeof testInterface !== "undefined") {
publicMethods.testInterface = testInterface;
}
// Initialize the main entropy pool instance on the main thread, only.
// I want only the main thread to create and manage the central entropy pool.
// All workers would have their own PRNG instance initialized by injected entropy from the main thread.
var entropyPool;
if (!runningInWorkerInstance) {
entropyPool = entropyPool || new MsrcryptoEntropy();
// Initialize the entropy pool in the main thread.
// There is only one entropy pool.
entropyPool.init();
var localEntropy = entropyPool.read(48); // 48 is from SP800-90A; could be longer
msrcryptoPseudoRandom.init(localEntropy);
}
return publicMethods;
})();