1
0
Fork 0

Added HMAC-SHA224, exposed SHA1, SHA224.

This commit is contained in:
Alan Plum 2014-06-24 12:48:42 +02:00
parent 42643c6b50
commit d2f0b0efc5
7 changed files with 228 additions and 19 deletions

View File

@ -1,15 +1,15 @@
/*jslint indent: 2, nomen: true, maxlen: 120, vars: true, white: true, plusplus: true, nonpropdel: true, proto: true */
/*jslint sloppy: true, regexp: true */
/*global require, module, Module, ArangoError, SleepAndRequeue,
REPLICATION_LOGGER_STATE, REPLICATION_LOGGER_CONFIGURE, REPLICATION_APPLIER_CONFIGURE, REPLICATION_APPLIER_START,
REPLICATION_LOGGER_STATE, REPLICATION_LOGGER_CONFIGURE, REPLICATION_APPLIER_CONFIGURE, REPLICATION_APPLIER_START,
REPLICATION_APPLIER_STOP, REPLICATION_APPLIER_FORGET, REPLICATION_APPLIER_STATE,
REPLICATION_SYNCHRONISE, REPLICATION_SERVER_ID, CONFIGURE_ENDPOINT, REMOVE_ENDPOINT, LIST_ENDPOINTS,
SYS_BASE64DECODE, SYS_BASE64ENCODE, SYS_DEBUG_SEGFAULT,
SYS_DEBUG_CAN_USE_FAILAT, SYS_DEBUG_SET_FAILAT, SYS_DEBUG_REMOVE_FAILAT, SYS_DEBUG_CLEAR_FAILAT,
SYS_DOWNLOAD, SYS_EXECUTE, SYS_GET_CURRENT_REQUEST, SYS_GET_CURRENT_RESPONSE,
SYS_DEBUG_CAN_USE_FAILAT, SYS_DEBUG_SET_FAILAT, SYS_DEBUG_REMOVE_FAILAT, SYS_DEBUG_CLEAR_FAILAT,
SYS_DOWNLOAD, SYS_EXECUTE, SYS_GET_CURRENT_REQUEST, SYS_GET_CURRENT_RESPONSE,
SYS_LOAD, SYS_LOG_LEVEL, SYS_MD5, SYS_OUTPUT, SYS_PROCESS_STATISTICS,
SYS_RAND, SYS_SERVER_STATISTICS, SYS_SPRINTF, SYS_TIME, SYS_START_PAGER, SYS_STOP_PAGER,
SYS_HMAC, SYS_SHA256, SYS_SLEEP, SYS_WAIT, SYS_PARSE, SYS_IMPORT_CSV_FILE, SYS_IMPORT_JSON_FILE, SYS_LOG,
SYS_RAND, SYS_SERVER_STATISTICS, SYS_SPRINTF, SYS_TIME, SYS_START_PAGER, SYS_STOP_PAGER,
SYS_HMAC, SYS_SHA256, SYS_SHA224, SYS_SHA1, SYS_SLEEP, SYS_WAIT, SYS_PARSE, SYS_IMPORT_CSV_FILE, SYS_IMPORT_JSON_FILE, SYS_LOG,
SYS_GEN_RANDOM_NUMBERS, SYS_GEN_RANDOM_ALPHA_NUMBERS, SYS_GEN_RANDOM_SALT, SYS_CREATE_NONCE,
SYS_CHECK_AND_MARK_NONCE, SYS_CLIENT_STATISTICS, SYS_HTTP_STATISTICS, SYS_UNIT_TESTS, SYS_UNIT_TESTS_RESULT:true,
SYS_PROCESS_CSV_FILE, SYS_PROCESS_JSON_FILE, ARANGO_QUIET, COLORS, COLOR_OUTPUT,
@ -81,10 +81,10 @@
this.message = this.toString();
};
exports.ArangoError.prototype = new Error();
}
exports.ArangoError.prototype = new Error();
}
exports.ArangoError.prototype._PRINT = function (context) {
context.output += this.toString();
};
@ -613,6 +613,24 @@
delete SYS_SHA256;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief sha224
////////////////////////////////////////////////////////////////////////////////
if (typeof SYS_SHA224 !== "undefined") {
exports.sha224 = SYS_SHA224;
delete SYS_SHA224;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief sha1
////////////////////////////////////////////////////////////////////////////////
if (typeof SYS_SHA1 !== "undefined") {
exports.sha1 = SYS_SHA1;
delete SYS_SHA1;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief serverStatistics
////////////////////////////////////////////////////////////////////////////////

View File

@ -1,15 +1,15 @@
/*jslint indent: 2, nomen: true, maxlen: 120, vars: true, white: true, plusplus: true, nonpropdel: true, proto: true */
/*jslint sloppy: true, regexp: true */
/*global require, module, Module, ArangoError, SleepAndRequeue,
REPLICATION_LOGGER_STATE, REPLICATION_LOGGER_CONFIGURE, REPLICATION_APPLIER_CONFIGURE, REPLICATION_APPLIER_START,
REPLICATION_LOGGER_STATE, REPLICATION_LOGGER_CONFIGURE, REPLICATION_APPLIER_CONFIGURE, REPLICATION_APPLIER_START,
REPLICATION_APPLIER_STOP, REPLICATION_APPLIER_FORGET, REPLICATION_APPLIER_STATE,
REPLICATION_SYNCHRONISE, REPLICATION_SERVER_ID, CONFIGURE_ENDPOINT, REMOVE_ENDPOINT, LIST_ENDPOINTS,
SYS_BASE64DECODE, SYS_BASE64ENCODE, SYS_DEBUG_SEGFAULT,
SYS_DEBUG_CAN_USE_FAILAT, SYS_DEBUG_SET_FAILAT, SYS_DEBUG_REMOVE_FAILAT, SYS_DEBUG_CLEAR_FAILAT,
SYS_DOWNLOAD, SYS_EXECUTE, SYS_GET_CURRENT_REQUEST, SYS_GET_CURRENT_RESPONSE,
SYS_DEBUG_CAN_USE_FAILAT, SYS_DEBUG_SET_FAILAT, SYS_DEBUG_REMOVE_FAILAT, SYS_DEBUG_CLEAR_FAILAT,
SYS_DOWNLOAD, SYS_EXECUTE, SYS_GET_CURRENT_REQUEST, SYS_GET_CURRENT_RESPONSE,
SYS_LOAD, SYS_LOG_LEVEL, SYS_MD5, SYS_OUTPUT, SYS_PROCESS_STATISTICS,
SYS_RAND, SYS_SERVER_STATISTICS, SYS_SPRINTF, SYS_TIME, SYS_START_PAGER, SYS_STOP_PAGER,
SYS_HMAC, SYS_SHA256, SYS_SLEEP, SYS_WAIT, SYS_PARSE, SYS_IMPORT_CSV_FILE, SYS_IMPORT_JSON_FILE, SYS_LOG,
SYS_RAND, SYS_SERVER_STATISTICS, SYS_SPRINTF, SYS_TIME, SYS_START_PAGER, SYS_STOP_PAGER,
SYS_HMAC, SYS_SHA256, SYS_SHA224, SYS_SHA1, SYS_SLEEP, SYS_WAIT, SYS_PARSE, SYS_IMPORT_CSV_FILE, SYS_IMPORT_JSON_FILE, SYS_LOG,
SYS_GEN_RANDOM_NUMBERS, SYS_GEN_RANDOM_ALPHA_NUMBERS, SYS_GEN_RANDOM_SALT, SYS_CREATE_NONCE,
SYS_CHECK_AND_MARK_NONCE, SYS_CLIENT_STATISTICS, SYS_HTTP_STATISTICS, SYS_UNIT_TESTS, SYS_UNIT_TESTS_RESULT:true,
SYS_PROCESS_CSV_FILE, SYS_PROCESS_JSON_FILE, ARANGO_QUIET, COLORS, COLOR_OUTPUT,
@ -81,10 +81,10 @@
this.message = this.toString();
};
exports.ArangoError.prototype = new Error();
}
exports.ArangoError.prototype = new Error();
}
exports.ArangoError.prototype._PRINT = function (context) {
context.output += this.toString();
};
@ -613,6 +613,24 @@
delete SYS_SHA256;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief sha224
////////////////////////////////////////////////////////////////////////////////
if (typeof SYS_SHA224 !== "undefined") {
exports.sha224 = SYS_SHA224;
delete SYS_SHA224;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief sha1
////////////////////////////////////////////////////////////////////////////////
if (typeof SYS_SHA1 !== "undefined") {
exports.sha1 = SYS_SHA1;
delete SYS_SHA1;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief serverStatistics
////////////////////////////////////////////////////////////////////////////////

View File

@ -81,6 +81,22 @@ exports.sha256 = function (value) {
return internal.sha256(value);
};
////////////////////////////////////////////////////////////////////////////////
/// @brief apply an SHA 224 hash
////////////////////////////////////////////////////////////////////////////////
exports.sha224 = function (value) {
return internal.sha224(value);
};
////////////////////////////////////////////////////////////////////////////////
/// @brief apply an SHA 1 hash
////////////////////////////////////////////////////////////////////////////////
exports.sha1 = function (value) {
return internal.sha1(value);
};
////////////////////////////////////////////////////////////////////////////////
/// @brief Generates a string of a given length containing numbers.
////////////////////////////////////////////////////////////////////////////////

View File

@ -105,6 +105,72 @@ function CryptoSuite () {
});
},
////////////////////////////////////////////////////////////////////////////////
/// @brief test sha224, invalid values
////////////////////////////////////////////////////////////////////////////////
testSha224Invalid : function () {
[ undefined, null, true, false, 0, 1, -1, 32.5, [ ], { } ].forEach(function (value) {
try {
crypto.sha224(value);
fail();
}
catch (err) {
}
});
},
////////////////////////////////////////////////////////////////////////////////
/// @brief test sha224
////////////////////////////////////////////////////////////////////////////////
testSha224 : function () {
var data = [
[ "", "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f" ],
[ " ", "ca17734c016e36b898af29c1aeb142e774abf4b70bac55ec98a27ba8"],
[ "arangodb", "deeb6d8f9b6c316e7f5a601fb2549a2ebc857bee78df38b1977c9989" ],
[ "Arangodb", "6d7a8a7fb22537dab437c8c2915874a170b2b14eb1aa787df32d6999" ],
[ "ArangoDB is a database", "9a3e02d47eb686c67f6b9a51efe16e8b4f88b0ee14248636d6163f1d" ]
];
data.forEach(function (value) {
assertEqual(value[1], crypto.sha224(value[0]));
});
},
////////////////////////////////////////////////////////////////////////////////
/// @brief test sha1, invalid values
////////////////////////////////////////////////////////////////////////////////
testSha1Invalid : function () {
[ undefined, null, true, false, 0, 1, -1, 32.5, [ ], { } ].forEach(function (value) {
try {
crypto.sha1(value);
fail();
}
catch (err) {
}
});
},
////////////////////////////////////////////////////////////////////////////////
/// @brief test sha1
////////////////////////////////////////////////////////////////////////////////
testSha1 : function () {
var data = [
[ "", "da39a3ee5e6b4b0d3255bfef95601890afd80709" ],
[ " ", "b858cb282617fb0956d960215c8e84d1ccf909c6"],
[ "arangodb", "1f9aa6577198dd5fcab487d08d45258608dac9b5" ],
[ "Arangodb", "5fc1b451c5cd4770df14bd3ae362b5587a195311" ],
[ "ArangoDB is a database", "9e45475b50ea3e8438c55919238aa5b0736bda43" ]
];
data.forEach(function (value) {
assertEqual(value[1], crypto.sha1(value[0]));
});
},
////////////////////////////////////////////////////////////////////////////////
/// @brief test hmac, invalid values
////////////////////////////////////////////////////////////////////////////////
@ -142,6 +208,8 @@ function CryptoSuite () {
[ "secret", "Arangodb", "sha256", "95144e880bbc4a4bf10a2b683603c763a38817b544e1c2f6ff1bd3523bf60f9e" ],
[ "secret", "ArangoDB is a database", "sha256", "4888d586d3208ca18ebaf78569949a13f3c03585edb007771cd820820a351b0f" ],
[ "SECRET", "ArangoDB is a database", "sha256", "a04df5ce362f49439db5e30032b20e0fa64d01c60ceb32a9150e58d3c2c929af" ],
[ "secret", "ArangoDB is a database", "sha224", "b55c13e25227abf919b510cf2289f4501fa13584676e7e4d56108172" ],
[ "secret", "ArangoDB is a database", "SHA224", "b55c13e25227abf919b510cf2289f4501fa13584676e7e4d56108172" ],
[ "secret", "ArangoDB is a database", "sha1", "f39d7a76e502ba3f79d663cfbc9ac43eb6fd323e" ],
[ "secret", "ArangoDB is a database", "SHA1", "f39d7a76e502ba3f79d663cfbc9ac43eb6fd323e" ],
[ "secret", "ArangoDB is a database", "md5", "6eecfc947725974efc24bbaaafe15a13" ],

View File

@ -190,6 +190,9 @@ namespace triagens {
if (algorithm == Algorithm::ALGORITHM_SHA1) {
evp_md = const_cast<EVP_MD*>(EVP_sha1());
}
else if (algorithm == Algorithm::ALGORITHM_SHA224) {
evp_md = const_cast<EVP_MD*>(EVP_sha224());
}
else if (algorithm == Algorithm::ALGORITHM_MD5) {
evp_md = const_cast<EVP_MD*>(EVP_md5());
}

View File

@ -39,7 +39,8 @@ namespace triagens {
enum Algorithm {
ALGORITHM_SHA256 = 0,
ALGORITHM_SHA1 = 1,
ALGORITHM_MD5 = 2
ALGORITHM_MD5 = 2,
ALGORITHM_SHA224 = 3
};
//////////////////////////////////////////////////////////////////////////

View File

@ -2273,6 +2273,86 @@ static v8::Handle<v8::Value> JS_Sha256 (v8::Arguments const& argv) {
return scope.Close(hashStr);
}
////////////////////////////////////////////////////////////////////////////////
/// @brief computes the sha224 sum
///
/// @FUN{internal.sha224(@FA{text})}
///
/// Computes an sha224 for the @FA{text}.
////////////////////////////////////////////////////////////////////////////////
static v8::Handle<v8::Value> JS_Sha224 (v8::Arguments const& argv) {
v8::HandleScope scope;
// extract arguments
if (argv.Length() != 1 || ! argv[0]->IsString()) {
TRI_V8_EXCEPTION_USAGE(scope, "sha224(<text>)");
}
string key = TRI_ObjectToString(argv[0]);
// create sha224
char* hash = 0;
size_t hashLen;
SslInterface::sslSHA224(key.c_str(), key.size(), hash, hashLen);
// as hex
char* hex = 0;
size_t hexLen;
SslInterface::sslHEX(hash, hashLen, hex, hexLen);
delete[] hash;
// and return
v8::Handle<v8::String> hashStr = v8::String::New(hex, (int) hexLen);
delete[] hex;
return scope.Close(hashStr);
}
////////////////////////////////////////////////////////////////////////////////
/// @brief computes the sha1 sum
///
/// @FUN{internal.sha1(@FA{text})}
///
/// Computes an sha1 for the @FA{text}.
////////////////////////////////////////////////////////////////////////////////
static v8::Handle<v8::Value> JS_Sha1 (v8::Arguments const& argv) {
v8::HandleScope scope;
// extract arguments
if (argv.Length() != 1 || ! argv[0]->IsString()) {
TRI_V8_EXCEPTION_USAGE(scope, "sha1(<text>)");
}
string key = TRI_ObjectToString(argv[0]);
// create sha1
char* hash = 0;
size_t hashLen;
SslInterface::sslSHA1(key.c_str(), key.size(), hash, hashLen);
// as hex
char* hex = 0;
size_t hexLen;
SslInterface::sslHEX(hash, hashLen, hex, hexLen);
delete[] hash;
// and return
v8::Handle<v8::String> hashStr = v8::String::New(hex, (int) hexLen);
delete[] hex;
return scope.Close(hashStr);
}
////////////////////////////////////////////////////////////////////////////////
/// @brief sleeps
///
@ -2565,6 +2645,9 @@ static v8::Handle<v8::Value> JS_HMAC (v8::Arguments const& argv) {
else if (algorithm == "sha256") {
al = SslInterface::Algorithm::ALGORITHM_SHA256;
}
else if (algorithm == "sha224") {
al = SslInterface::Algorithm::ALGORITHM_SHA224;
}
else if (algorithm == "md5") {
al = SslInterface::Algorithm::ALGORITHM_MD5;
}
@ -3391,6 +3474,8 @@ void TRI_InitV8Utils (v8::Handle<v8::Context> context,
TRI_AddGlobalFunctionVocbase(context, "SYS_SAVE", JS_Save);
TRI_AddGlobalFunctionVocbase(context, "SYS_SERVER_STATISTICS", JS_ServerStatistics);
TRI_AddGlobalFunctionVocbase(context, "SYS_SHA256", JS_Sha256);
TRI_AddGlobalFunctionVocbase(context, "SYS_SHA224", JS_Sha224);
TRI_AddGlobalFunctionVocbase(context, "SYS_SHA1", JS_Sha1);
TRI_AddGlobalFunctionVocbase(context, "SYS_SLEEP", JS_Sleep);
TRI_AddGlobalFunctionVocbase(context, "SYS_SPRINTF", JS_SPrintF);
TRI_AddGlobalFunctionVocbase(context, "SYS_STATUS_EXTERNAL", JS_StatusExternal);