Skip to content

Commit 043046d

Browse files
authored
Deprecate crypto.to{Hex,Base64} (#2929)
The internal implementation already preferentially forwards to the encoder module, so we should just remove these functions as they confuse people into thinking that we don't have their inverses (see the feature request #2907). Update the docs to refer to the encoder version and add deprecation warnings to the runtime implementations.
1 parent 04287ac commit 043046d

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

app/modules/crypto.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,11 @@ static int call_encoder( lua_State* L, const char *function ) {
6161
}
6262

6363
static int crypto_base64_encode (lua_State* L) {
64+
platform_print_deprecation_note("crypto.toBase64", "in the next version");
6465
return call_encoder(L, "toBase64");
6566
}
6667
static int crypto_hex_encode (lua_State* L) {
68+
platform_print_deprecation_note("crypto.toHex", "in the next version");
6769
return call_encoder(L, "toHex");
6870
}
6971
#else
@@ -79,6 +81,8 @@ static int crypto_base64_encode( lua_State* L )
7981
const char* msg = luaL_checklstring(L, 1, &len);
8082
luaL_Buffer out;
8183

84+
platform_print_deprecation_note("crypto.toBase64", "in the next version");
85+
8286
luaL_buffinit(L, &out);
8387
for (i = 0; i < len; i += 3) {
8488
int a = msg[i];
@@ -104,6 +108,8 @@ static int crypto_hex_encode( lua_State* L)
104108
const char* msg = luaL_checklstring(L, 1, &len);
105109
luaL_Buffer out;
106110

111+
platform_print_deprecation_note("crypto.toHex", "in the next version");
112+
107113
luaL_buffinit(L, &out);
108114
for (i = 0; i < len; i++) {
109115
luaL_addchar(&out, crypto_hexbytes[msg[i] >> 4]);

docs/modules/crypto.md

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ The encrypted data as a binary string. For AES this is always a multiple of 16 b
3333

3434
#### Example
3535
```lua
36-
print(crypto.toHex(crypto.encrypt("AES-ECB", "1234567890abcdef", "Hi, I'm secret!")))
36+
print(encoder.toHex(crypto.encrypt("AES-ECB", "1234567890abcdef", "Hi, I'm secret!")))
3737
```
3838

3939
#### See also
@@ -62,7 +62,7 @@ Note that the decrypted string may contain extra zero-bytes of padding at the en
6262
```lua
6363
key = "1234567890abcdef"
6464
cipher = crypto.encrypt("AES-ECB", key, "Hi, I'm secret!")
65-
print(crypto.toHex(cipher))
65+
print(encoder.toHex(cipher))
6666
print(crypto.decrypt("AES-ECB", key, cipher))
6767
```
6868

@@ -82,11 +82,11 @@ Compute a cryptographic hash of a a file.
8282
- `filename` the path to the file to hash
8383

8484
#### Returns
85-
A binary string containing the message digest. To obtain the textual version (ASCII hex characters), please use [`crypto.toHex()`](#cryptotohex ).
85+
A binary string containing the message digest. To obtain the textual version (ASCII hex characters), please use [`encoder.toHex()`](encoder.md#encodertohex ).
8686

8787
#### Example
8888
```lua
89-
print(crypto.toHex(crypto.fhash("sha1","myfile.lua")))
89+
print(encoder.toHex(crypto.fhash("sha1","myfile.lua")))
9090
```
9191

9292
## crypto.hash()
@@ -101,11 +101,11 @@ Compute a cryptographic hash of a Lua string.
101101
`str` string to hash contents of
102102

103103
#### Returns
104-
A binary string containing the message digest. To obtain the textual version (ASCII hex characters), please use [`crypto.toHex()`](#cryptotohex ).
104+
A binary string containing the message digest. To obtain the textual version (ASCII hex characters), please use [`encoder.toHex()`](encoder.md#encodertohex).
105105

106106
#### Example
107107
```lua
108-
print(crypto.toHex(crypto.hash("sha1","abc")))
108+
print(encoder.toHex(crypto.hash("sha1","abc")))
109109
```
110110

111111
## crypto.new_hash()
@@ -127,7 +127,7 @@ hashobj = crypto.new_hash("SHA1")
127127
hashobj:update("FirstString")
128128
hashobj:update("SecondString")
129129
digest = hashobj:finalize()
130-
print(crypto.toHex(digest))
130+
print(encoder.toHex(digest))
131131
```
132132

133133
## crypto.hmac()
@@ -143,11 +143,11 @@ Compute a [HMAC](https://en.wikipedia.org/wiki/Hash-based_message_authentication
143143
- `key` key to use for signing, may be a binary string
144144

145145
#### Returns
146-
A binary string containing the HMAC signature. Use [`crypto.toHex()`](#cryptotohex) to obtain the textual version.
146+
A binary string containing the HMAC signature. Use [`encoder.toHex()`](encoder.md#encodertohex) to obtain the textual version.
147147

148148
#### Example
149149
```lua
150-
print(crypto.toHex(crypto.hmac("sha1","abc","mysecret")))
150+
print(encoder.toHex(crypto.hmac("sha1","abc","mysecret")))
151151
```
152152

153153
## crypto.new_hmac()
@@ -170,7 +170,7 @@ hmacobj = crypto.new_hmac("SHA1", "s3kr3t")
170170
hmacobj:update("FirstString")
171171
hmacobj:update("SecondString")
172172
digest = hmacobj:finalize()
173-
print(crypto.toHex(digest))
173+
print(encoder.toHex(digest))
174174
```
175175

176176

@@ -186,17 +186,21 @@ Applies an XOR mask to a Lua string. Note that this is not a proper cryptographi
186186
- `mask` the mask to apply, repeated if shorter than the message
187187

188188
#### Returns
189-
The masked message, as a binary string. Use [`crypto.toHex()`](#cryptotohex) to get a textual representation of it.
189+
The masked message, as a binary string. Use [`encoder.toHex()`](encoder.md#encodertohex) to get a textual representation of it.
190190

191191
#### Example
192192
```lua
193-
print(crypto.toHex(crypto.mask("some message to obscure","X0Y7")))
193+
print(encoder.toHex(crypto.mask("some message to obscure","X0Y7")))
194194
```
195195

196196
## crypto.toBase64()
197197

198198
Provides a Base64 representation of a (binary) Lua string.
199199

200+
!!! warning
201+
202+
This function is deprecated; please use instead [`encoder.toBase64()`](encoder.md#encodertobase64)
203+
200204
#### Syntax
201205
`b64 = crypto.toBase64(binary)`
202206

@@ -215,6 +219,10 @@ print(crypto.toBase64(crypto.hash("sha1","abc")))
215219

216220
Provides an ASCII hex representation of a (binary) Lua string. Each byte in the input string is represented as two hex characters in the output.
217221

222+
!!! warning
223+
224+
This function is deprecated; please use instead [`encoder.toHex()`](encoder.md#encodertohex)
225+
218226
#### Syntax
219227
`hexstr = crypto.toHex(binary)`
220228

0 commit comments

Comments
 (0)