From 52cc7446b13b1aacd64feeaf57ca8653a7257faa Mon Sep 17 00:00:00 2001 From: Chris Jones Date: Tue, 14 Mar 2023 12:13:31 +0000 Subject: [PATCH] Add binary functions to hs.hash for MD5, SHA1, SHA256 and SHA512 --- extensions/hash/libhash.m | 104 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 100 insertions(+), 4 deletions(-) diff --git a/extensions/hash/libhash.m b/extensions/hash/libhash.m index bf1a6415e..5c85fe9b0 100644 --- a/extensions/hash/libhash.m +++ b/extensions/hash/libhash.m @@ -53,7 +53,7 @@ static int doHashHMAC(lua_State *L, CCHmacAlgorithm algorithm, CC_LONG resultLen /// * data - A string containing some data to hash /// /// Returns: -/// * A string containing the hash of the supplied data +/// * A string containing the hash of the supplied data, encoded as hexadecimal static int hash_sha1(lua_State *L) { return doHash(L, CC_SHA1_DIGEST_LENGTH, CC_SHA1); } @@ -66,7 +66,7 @@ static int hash_sha1(lua_State *L) { /// * data - A string containing some data to hash /// /// Returns: -/// * A string containing the hash of the supplied data +/// * A string containing the hash of the supplied data, encoded as hexadecimal static int hash_sha256(lua_State *L) { return doHash(L, CC_SHA256_DIGEST_LENGTH, CC_SHA256); } @@ -79,7 +79,7 @@ static int hash_sha256(lua_State *L) { /// * data - A string containing some data to hash /// /// Returns: -/// * A string containing the hash of the supplied data +/// * A string containing the hash of the supplied data, encoded as hexadecimal static int hash_sha512(lua_State *L) { return doHash(L, CC_SHA512_DIGEST_LENGTH, CC_SHA512); } @@ -92,7 +92,7 @@ static int hash_sha512(lua_State *L) { /// * data - A string containing some data to hash /// /// Returns: -/// * A string containing the hash of the supplied data +/// * A string containing the hash of the supplied data, encoded as hexadecimal static int hash_md5(lua_State *L) { #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" @@ -100,6 +100,97 @@ static int hash_md5(lua_State *L) { #pragma clang diagnostic pop } +/// hs.hash.bSHA1(data) -> data +/// Function +/// Calculates a binary SHA1 hash +/// +/// Parameters: +/// * data - A string containing some data to hash +/// +/// Returns: +/// * A string containing the binary hash of the supplied data +static int hash_bsha1(lua_State *L) { + LuaSkin *skin = [LuaSkin sharedWithState:L]; + [skin checkArgs:LS_TSTRING, LS_TBREAK]; + + NSData *dataIn = [skin toNSObjectAtIndex:1 withOptions:LS_NSLuaStringAsDataOnly]; + + NSMutableData *macOut = [NSMutableData dataWithLength:CC_SHA1_DIGEST_LENGTH]; + CC_SHA1(dataIn.bytes, (CC_LONG)dataIn.length, macOut.mutableBytes); + + [skin pushNSObject:macOut]; + return 1; +} + +/// hs.hash.bSHA256(data) -> data +/// Function +/// Calculates a binary SHA256 hash +/// +/// Parameters: +/// * data - A string containing some data to hash +/// +/// Returns: +/// * A string containing the binary hash of the supplied data +static int hash_bsha256(lua_State *L) { + LuaSkin *skin = [LuaSkin sharedWithState:L]; + [skin checkArgs:LS_TSTRING, LS_TBREAK]; + + NSData *dataIn = [skin toNSObjectAtIndex:1 withOptions:LS_NSLuaStringAsDataOnly]; + + NSMutableData *macOut = [NSMutableData dataWithLength:CC_SHA256_DIGEST_LENGTH]; + CC_SHA256(dataIn.bytes, (CC_LONG)dataIn.length, macOut.mutableBytes); + + [skin pushNSObject:macOut]; + return 1; +} + +/// hs.hash.bSHA512(data) -> data +/// Function +/// Calculates a binary SHA512 hash +/// +/// Parameters: +/// * data - A string containing some data to hash +/// +/// Returns: +/// * A string containing the binary hash of the supplied data +static int hash_bsha512(lua_State *L) { + LuaSkin *skin = [LuaSkin sharedWithState:L]; + [skin checkArgs:LS_TSTRING, LS_TBREAK]; + + NSData *dataIn = [skin toNSObjectAtIndex:1 withOptions:LS_NSLuaStringAsDataOnly]; + + NSMutableData *macOut = [NSMutableData dataWithLength:CC_SHA512_DIGEST_LENGTH]; + CC_SHA512(dataIn.bytes, (CC_LONG)dataIn.length, macOut.mutableBytes); + + [skin pushNSObject:macOut]; + return 1; +} + +/// hs.hash.bMD5(data) -> data +/// Function +/// Calculates a binary MD5 hash +/// +/// Parameters: +/// * data - A string containing some data to hash +/// +/// Returns: +/// * A string containing the binary hash of the supplied data +static int hash_bmd5(lua_State *L) { + LuaSkin *skin = [LuaSkin sharedWithState:L]; + [skin checkArgs:LS_TSTRING, LS_TBREAK]; + + NSData *dataIn = [skin toNSObjectAtIndex:1 withOptions:LS_NSLuaStringAsDataOnly]; + + NSMutableData *macOut = [NSMutableData dataWithLength:CC_MD5_DIGEST_LENGTH]; +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" + CC_MD5(dataIn.bytes, (CC_LONG)dataIn.length, macOut.mutableBytes); +#pragma clang diagnostic pop + + [skin pushNSObject:macOut]; + return 1; +} + /// hs.hash.hmacSHA1(key, data) -> string /// Function /// Calculates an HMAC using a key and a SHA1 hash @@ -162,6 +253,11 @@ static int hash_md5_hmac(lua_State *L) { {"SHA512", hash_sha512}, {"MD5", hash_md5}, + {"bSHA1", hash_bsha1}, + {"bSHA256", hash_bsha256}, + {"bSHA512", hash_bsha512}, + {"bMD5", hash_bmd5}, + {"hmacSHA1", hash_sha1_hmac}, {"hmacSHA256", hash_sha256_hmac}, {"hmacSHA512", hash_sha512_hmac},