goog.provide('wycheproof.TestUtil');
goog.require('goog.crypt');
goog.require('goog.crypt.base64');
goog.require('goog.json');
wycheproof.TestUtil.readJsonTestVectorsFromFile = function(filename){
var fileContent = goog.loadFileSync_(filename);
assertTrue('Invalid file format (expected JSON)', goog.json.isValid(fileContent));
return JSON.parse(fileContent);
};
wycheproof.TestUtil.checkTestCaseResult = function(result) {
var failMsg = '';
if (result.errors.length > 0) {
for (var i = 0; i < result.errors.length; i++) {
failMsg += result.errors[i].message + '\n';
}
fail(failMsg);
}
};
wycheproof.TestUtil.isHex = function(s) {
return /(^[0-9A-F]*$)|(^[0-9a-f]*$)/.test(s);
};
wycheproof.TestUtil.hexToArrayBuffer = function(s) {
var bytes = goog.crypt.hexToByteArray(s);
return new Uint8Array(bytes).buffer;
};
wycheproof.TestUtil.arrayBufferToHex = function(ab) {
return goog.crypt.byteArrayToHex(new Uint8Array(ab));
};
wycheproof.TestUtil.base64UrlToHex = function (s) {
var b64str = wycheproof.TestUtil.base64UrlToBase64(s);
return wycheproof.TestUtil.base64ToHex(b64str);
};
wycheproof.TestUtil.hexToBase64 = function(s) {
var bytes = goog.crypt.hexToByteArray(s);
return goog.crypt.base64.encodeByteArray(bytes);
};
wycheproof.TestUtil.base64ToHex = function(s) {
var bytes = goog.crypt.base64.decodeStringToByteArray(s);
return goog.crypt.byteArrayToHex(bytes);
};
wycheproof.TestUtil.base64ToBase64Url = function(s) {
return s.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=/g, '');
};
wycheproof.TestUtil.base64UrlToBase64 = function(s) {
return (s + '==='.slice((s.length + 3) % 4))
.replace(/\-/g, '+')
.replace(/_/g, '/');
};