Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crypto: remove toHex and toBase64 #3300

Merged
merged 1 commit into from
Sep 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 4 additions & 80 deletions app/modules/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,80 +44,6 @@ static int crypto_sha1( lua_State* L )
return 1;
}

#ifdef LUA_USE_MODULES_ENCODER
static int call_encoder( lua_State* L, const char *function ) {
if (lua_gettop(L) != 1) {
luaL_error(L, "%s must have one argument", function);
}
lua_getglobal(L, "encoder");
luaL_checktype(L, -1, LUA_TTABLE);
lua_getfield(L, -1, function);
lua_insert(L, 1); //move function below the argument
lua_pop(L, 1); //and dump the encoder rotable from stack.
lua_call(L,1,1); // Normal call encoder.xxx(string)
// (errors thrown back to caller)
return 1;
}

static int crypto_base64_encode (lua_State* L) {
platform_print_deprecation_note("crypto.toBase64", "in the next version");
return call_encoder(L, "toBase64");
}
static int crypto_hex_encode (lua_State* L) {
platform_print_deprecation_note("crypto.toHex", "in the next version");
return call_encoder(L, "toHex");
}
#else
static const char* bytes64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
/**
* encoded = crypto.toBase64(raw)
*
* Encodes raw binary string as base64 string.
*/
static int crypto_base64_encode( lua_State* L )
{
int len, i;
const char* msg = luaL_checklstring(L, 1, &len);
luaL_Buffer out;

platform_print_deprecation_note("crypto.toBase64", "in the next version");

luaL_buffinit(L, &out);
for (i = 0; i < len; i += 3) {
int a = msg[i];
int b = (i + 1 < len) ? msg[i + 1] : 0;
int c = (i + 2 < len) ? msg[i + 2] : 0;
luaL_addchar(&out, bytes64[a >> 2]);
luaL_addchar(&out, bytes64[((a & 3) << 4) | (b >> 4)]);
luaL_addchar(&out, (i + 1 < len) ? bytes64[((b & 15) << 2) | (c >> 6)] : 61);
luaL_addchar(&out, (i + 2 < len) ? bytes64[(c & 63)] : 61);
}
luaL_pushresult(&out);
return 1;
}

/**
* encoded = crypto.toHex(raw)
*
* Encodes raw binary string as hex string.
*/
static int crypto_hex_encode( lua_State* L)
{
int len, i;
const char* msg = luaL_checklstring(L, 1, &len);
luaL_Buffer out;

platform_print_deprecation_note("crypto.toHex", "in the next version");

luaL_buffinit(L, &out);
for (i = 0; i < len; i++) {
luaL_addchar(&out, crypto_hexbytes[msg[i] >> 4]);
luaL_addchar(&out, crypto_hexbytes[msg[i] & 0xf]);
}
luaL_pushresult(&out);
return 1;
}
#endif
/**
* masked = crypto.mask(message, mask)
*
Expand Down Expand Up @@ -147,7 +73,7 @@ static inline int bad_mem (lua_State *L) { return luaL_error (L, "insufficient
static inline int bad_file (lua_State *L) { return luaL_error (L, "file does not exist"); }

/* rawdigest = crypto.hash("MD5", str)
* strdigest = crypto.toHex(rawdigest)
* strdigest = encoder.toHex(rawdigest)
*/
static int crypto_lhash (lua_State *L)
{
Expand All @@ -169,7 +95,7 @@ static int crypto_lhash (lua_State *L)
* sha = crypto.new_hash("MD5")
* sha.update("Data")
* sha.update("Data2")
* strdigest = crypto.toHex(sha.finalize())
* strdigest = encoder.toHex(sha.finalize())
*/

#define WANT_HASH 0
Expand Down Expand Up @@ -279,7 +205,7 @@ static sint32_t vfs_read_wrap (int fd, void *ptr, size_t len)
}

/* rawdigest = crypto.hash("MD5", filename)
* strdigest = crypto.toHex(rawdigest)
* strdigest = encoder.toHex(rawdigest)
*/
static int crypto_flhash (lua_State *L)
{
Expand Down Expand Up @@ -313,7 +239,7 @@ static int crypto_flhash (lua_State *L)


/* rawsignature = crypto.hmac("SHA1", str, key)
* strsignature = crypto.toHex(rawsignature)
* strsignature = encoder.toHex(rawsignature)
*/
static int crypto_lhmac (lua_State *L)
{
Expand Down Expand Up @@ -400,8 +326,6 @@ LROT_END(crypto_hash_map, NULL, LROT_MASK_INDEX)
// Module function map
LROT_BEGIN(crypto, NULL, 0)
LROT_FUNCENTRY( sha1, crypto_sha1 )
LROT_FUNCENTRY( toBase64, crypto_base64_encode )
LROT_FUNCENTRY( toHex, crypto_hex_encode )
LROT_FUNCENTRY( mask, crypto_mask )
LROT_FUNCENTRY( hash, crypto_lhash )
LROT_FUNCENTRY( fhash, crypto_flhash )
Expand Down
44 changes: 0 additions & 44 deletions docs/modules/crypto.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,47 +191,3 @@ The masked message, as a binary string. Use [`encoder.toHex()`](encoder.md#encod
```lua
print(encoder.toHex(crypto.mask("some message to obscure","X0Y7")))
```

## crypto.toBase64()

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

!!! warning

This function is deprecated; please use instead [`encoder.toBase64()`](encoder.md#encodertobase64)

#### Syntax
`b64 = crypto.toBase64(binary)`

#### Parameters
`binary` input string to Base64 encode

#### Return
A Base64 encoded string.

#### Example
```lua
print(crypto.toBase64(crypto.hash("sha1","abc")))
```

## crypto.toHex()

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.

!!! warning

This function is deprecated; please use instead [`encoder.toHex()`](encoder.md#encodertohex)

#### Syntax
`hexstr = crypto.toHex(binary)`

#### Parameters
`binary` input string to get hex representation for

#### Returns
An ASCII hex string.

#### Example
```lua
print(crypto.toHex(crypto.hash("sha1","abc")))
```