Skip to content

Commit c9d612e

Browse files
authored
Add binary functions to hs.hash for MD5, SHA1, SHA256 and SHA512 (#3428)
1 parent b3ffba9 commit c9d612e

File tree

1 file changed

+100
-4
lines changed

1 file changed

+100
-4
lines changed

extensions/hash/libhash.m

Lines changed: 100 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ static int doHashHMAC(lua_State *L, CCHmacAlgorithm algorithm, CC_LONG resultLen
5353
/// * data - A string containing some data to hash
5454
///
5555
/// Returns:
56-
/// * A string containing the hash of the supplied data
56+
/// * A string containing the hash of the supplied data, encoded as hexadecimal
5757
static int hash_sha1(lua_State *L) {
5858
return doHash(L, CC_SHA1_DIGEST_LENGTH, CC_SHA1);
5959
}
@@ -66,7 +66,7 @@ static int hash_sha1(lua_State *L) {
6666
/// * data - A string containing some data to hash
6767
///
6868
/// Returns:
69-
/// * A string containing the hash of the supplied data
69+
/// * A string containing the hash of the supplied data, encoded as hexadecimal
7070
static int hash_sha256(lua_State *L) {
7171
return doHash(L, CC_SHA256_DIGEST_LENGTH, CC_SHA256);
7272
}
@@ -79,7 +79,7 @@ static int hash_sha256(lua_State *L) {
7979
/// * data - A string containing some data to hash
8080
///
8181
/// Returns:
82-
/// * A string containing the hash of the supplied data
82+
/// * A string containing the hash of the supplied data, encoded as hexadecimal
8383
static int hash_sha512(lua_State *L) {
8484
return doHash(L, CC_SHA512_DIGEST_LENGTH, CC_SHA512);
8585
}
@@ -92,14 +92,105 @@ static int hash_sha512(lua_State *L) {
9292
/// * data - A string containing some data to hash
9393
///
9494
/// Returns:
95-
/// * A string containing the hash of the supplied data
95+
/// * A string containing the hash of the supplied data, encoded as hexadecimal
9696
static int hash_md5(lua_State *L) {
9797
#pragma clang diagnostic push
9898
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
9999
return doHash(L, CC_MD5_DIGEST_LENGTH, CC_MD5);
100100
#pragma clang diagnostic pop
101101
}
102102

103+
/// hs.hash.bSHA1(data) -> data
104+
/// Function
105+
/// Calculates a binary SHA1 hash
106+
///
107+
/// Parameters:
108+
/// * data - A string containing some data to hash
109+
///
110+
/// Returns:
111+
/// * A string containing the binary hash of the supplied data
112+
static int hash_bsha1(lua_State *L) {
113+
LuaSkin *skin = [LuaSkin sharedWithState:L];
114+
[skin checkArgs:LS_TSTRING, LS_TBREAK];
115+
116+
NSData *dataIn = [skin toNSObjectAtIndex:1 withOptions:LS_NSLuaStringAsDataOnly];
117+
118+
NSMutableData *macOut = [NSMutableData dataWithLength:CC_SHA1_DIGEST_LENGTH];
119+
CC_SHA1(dataIn.bytes, (CC_LONG)dataIn.length, macOut.mutableBytes);
120+
121+
[skin pushNSObject:macOut];
122+
return 1;
123+
}
124+
125+
/// hs.hash.bSHA256(data) -> data
126+
/// Function
127+
/// Calculates a binary SHA256 hash
128+
///
129+
/// Parameters:
130+
/// * data - A string containing some data to hash
131+
///
132+
/// Returns:
133+
/// * A string containing the binary hash of the supplied data
134+
static int hash_bsha256(lua_State *L) {
135+
LuaSkin *skin = [LuaSkin sharedWithState:L];
136+
[skin checkArgs:LS_TSTRING, LS_TBREAK];
137+
138+
NSData *dataIn = [skin toNSObjectAtIndex:1 withOptions:LS_NSLuaStringAsDataOnly];
139+
140+
NSMutableData *macOut = [NSMutableData dataWithLength:CC_SHA256_DIGEST_LENGTH];
141+
CC_SHA256(dataIn.bytes, (CC_LONG)dataIn.length, macOut.mutableBytes);
142+
143+
[skin pushNSObject:macOut];
144+
return 1;
145+
}
146+
147+
/// hs.hash.bSHA512(data) -> data
148+
/// Function
149+
/// Calculates a binary SHA512 hash
150+
///
151+
/// Parameters:
152+
/// * data - A string containing some data to hash
153+
///
154+
/// Returns:
155+
/// * A string containing the binary hash of the supplied data
156+
static int hash_bsha512(lua_State *L) {
157+
LuaSkin *skin = [LuaSkin sharedWithState:L];
158+
[skin checkArgs:LS_TSTRING, LS_TBREAK];
159+
160+
NSData *dataIn = [skin toNSObjectAtIndex:1 withOptions:LS_NSLuaStringAsDataOnly];
161+
162+
NSMutableData *macOut = [NSMutableData dataWithLength:CC_SHA512_DIGEST_LENGTH];
163+
CC_SHA512(dataIn.bytes, (CC_LONG)dataIn.length, macOut.mutableBytes);
164+
165+
[skin pushNSObject:macOut];
166+
return 1;
167+
}
168+
169+
/// hs.hash.bMD5(data) -> data
170+
/// Function
171+
/// Calculates a binary MD5 hash
172+
///
173+
/// Parameters:
174+
/// * data - A string containing some data to hash
175+
///
176+
/// Returns:
177+
/// * A string containing the binary hash of the supplied data
178+
static int hash_bmd5(lua_State *L) {
179+
LuaSkin *skin = [LuaSkin sharedWithState:L];
180+
[skin checkArgs:LS_TSTRING, LS_TBREAK];
181+
182+
NSData *dataIn = [skin toNSObjectAtIndex:1 withOptions:LS_NSLuaStringAsDataOnly];
183+
184+
NSMutableData *macOut = [NSMutableData dataWithLength:CC_MD5_DIGEST_LENGTH];
185+
#pragma clang diagnostic push
186+
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
187+
CC_MD5(dataIn.bytes, (CC_LONG)dataIn.length, macOut.mutableBytes);
188+
#pragma clang diagnostic pop
189+
190+
[skin pushNSObject:macOut];
191+
return 1;
192+
}
193+
103194
/// hs.hash.hmacSHA1(key, data) -> string
104195
/// Function
105196
/// Calculates an HMAC using a key and a SHA1 hash
@@ -162,6 +253,11 @@ static int hash_md5_hmac(lua_State *L) {
162253
{"SHA512", hash_sha512},
163254
{"MD5", hash_md5},
164255

256+
{"bSHA1", hash_bsha1},
257+
{"bSHA256", hash_bsha256},
258+
{"bSHA512", hash_bsha512},
259+
{"bMD5", hash_bmd5},
260+
165261
{"hmacSHA1", hash_sha1_hmac},
166262
{"hmacSHA256", hash_sha256_hmac},
167263
{"hmacSHA512", hash_sha512_hmac},

0 commit comments

Comments
 (0)