Skip to content
Snippets Groups Projects
Commit 0ca6cb87 authored by Veniamin Gvozdikov's avatar Veniamin Gvozdikov Committed by Roman Tsisyk
Browse files

Add string.hex() method

* Add string.hex() method
* Add hmac.*_hex to crypto.lua
* Update crypto/digest to use string.hex()

Closes #2510
parent 47c98418
No related branches found
No related tags found
No related merge requests found
-- crypto.lua (internal file)
local ffi = require 'ffi'
local ffi = require('ffi')
local buffer = require('buffer')
ffi.cdef[[
......@@ -354,6 +354,12 @@ for class, digest in pairs(hmacs) do
return res
end
})
hmac_api[class .. '_hex'] = function (key, str)
if type(str) ~= 'string' then
error("Usage: hmac."..class.."_hex(key, string)")
end
return string.hex(hmac_api[class](key, str))
end
end
hmac_api = setmetatable(hmac_api,
......
-- digest.lua (internal file)
local ffi = require 'ffi'
local ffi = require('ffi')
local crypto = require('crypto')
ffi.cdef[[
......@@ -39,16 +39,6 @@ local digest_shortcuts = {
md4 = 'MD4',
}
local hexres = ffi.new('char[129]')
local function str_to_hex(r)
for i = 0, r:len() - 1 do
ffi.C.snprintf(hexres + i * 2, 3, "%02x",
ffi.cast('unsigned char', r:byte(i + 1)))
end
return ffi.string(hexres, r:len() * 2)
end
local PMurHash
local PMurHash_methods = {
......@@ -190,7 +180,7 @@ local m = {
error("Usage: digest.sha1_hex(string)")
end
local r = ffi.C.SHA1internal(str, #str, nil)
return str_to_hex(ffi.string(r, 20))
return string.hex(ffi.string(r, 20))
end,
guava = function(state, buckets)
......@@ -217,7 +207,7 @@ for digest, name in pairs(digest_shortcuts) do
if type(str) ~= 'string' then
error('Usage: digest.'..digest..'_hex(string)')
end
return str_to_hex(crypto.digest[digest](str))
return string.hex(crypto.digest[digest](str))
end
end
......
......@@ -278,6 +278,20 @@ local function string_endswith(inp, tail, _start, _end)
return memcmp(c_char_ptr(inp) + _start, c_char_ptr(tail), tail_len) == 0
end
local function string_hex(inp)
if type(inp) ~= 'string' then
error(err_string_arg:format(1, 'string.hex', 'string', type(inp)), 2)
end
local len = inp:len() * 2
local res = ffi.new('char[?]', len + 1)
local uinp = ffi.cast('const unsigned char *', inp)
for i = 0, inp:len() - 1 do
ffi.C.snprintf(res + i * 2, 3, "%02x", ffi.cast('unsigned', uinp[i]))
end
return ffi.string(res, len)
end
-- It'll automatically set string methods, too.
local string = require('string')
string.split = string_split
......@@ -286,3 +300,4 @@ string.rjust = string_rjust
string.center = string_center
string.startswith = string_startswith
string.endswith = string_endswith
string.hex = string_hex
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment