Skip to content
Snippets Groups Projects
Commit 530f4a67 authored by Konstantin Osipov's avatar Konstantin Osipov
Browse files

Merge remote-tracking branch 'origin/gh-367-add-module-base64'

parents 6ac6d8cd 3199c132
No related branches found
No related tags found
No related merge requests found
......@@ -39,12 +39,6 @@ local GREETING_SIZE = 128
local TIMEOUT_INFINITY = 500 * 365 * 86400
ffi.cdef[[
int base64_decode(const char *in_base64, int in_len,
char *out_bin, int out_len);
]]
local sequence_mt = { __serialize = 'sequence'}
local mapping_mt = { __serialize = 'mapping'}
......@@ -75,13 +69,6 @@ local function strxor(s1, s2)
return res
end
local function b64decode(str)
local so = ffi.new('char[?]', string.len(str) * 2);
local len =
ffi.C.base64_decode(str, string.len(str), so, string.len(str) * 2)
return ffi.string(so, len)
end
local function keyfy(v)
if type(v) == 'table' then
return v
......@@ -215,7 +202,7 @@ local proto = {
auth = function(sync, user, password, handshake)
local saltb64 = string.sub(handshake, 65)
local salt = string.sub(b64decode(saltb64), 1, 20)
local salt = string.sub(digest.base64_decode(saltb64), 1, 20)
local hpassword = digest.sha1(password)
local hhpassword = digest.sha1(hpassword)
......@@ -228,7 +215,7 @@ local proto = {
)
end,
b64decode = b64decode,
b64decode = digest.base64_decode,
}
......
......@@ -25,6 +25,10 @@ ffi.cdef[[
typedef uint32_t (*crc32_func)(uint32_t crc,
const unsigned char *buf, unsigned int len);
extern crc32_func crc32_calc;
/* base64 */
int base64_decode(const char *in_base64, int in_len, char *out_bin, int out_len);
int base64_encode(const char *in_bin, int in_len, char *out_base64, int out_len);
]]
local ssl
......@@ -67,6 +71,37 @@ local function tohex(r, size)
end
local m = {
base64_encode = function(bin)
if bin == nil then
error('Usage: base64.encode(str)')
else
-- note: need add check size of bin, bin might containe any binary data
if type(bin) == 'string' then
local blen = string.len(bin)
local slen = math.ceil(blen * 4 / 3) + 2
local so = ffi.new('char[?]', slen)
local len = ffi.C.base64_encode(bin, blen, so, slen)
bin = ffi.string(so, len)
else
bin = ''
end
end
return bin
end,
base64_decode = function(data)
if type(data) ~= 'string' then
data = ''
else
local slen = string.len(data)
local blen = math.ceil(slen * 3 / 4)
local bo = ffi.new('char[?]', blen)
local len = ffi.C.base64_decode(data, slen, bo, blen)
data = ffi.string(bo, len)
end
return data
end,
crc32 = function(str)
if str == nil then
str = ''
......
......@@ -149,6 +149,46 @@ digest.crc32_update(digest.crc32('abc'), 'cde')
---
- 3628146660
...
digest.base64_encode('12345')
---
- MTIzNDU=
...
digest.base64_decode('MTIzNDU=')
---
- '12345'
...
digest.base64_encode('asdfl asdf adfa zxc vzxcvz llll')
---
- YXNkZmwgYXNkZiBhZGZhIHp4YyB2enhjdnogbGxsbA==
...
digest.base64_decode('YXNkZmwgYXNkZiBhZGZhIHp4YyB2enhjdnogbGxsbA==')
---
- asdfl asdf adfa zxc vzxcvz llll
...
digest.base64_encode('11 00 11 00 abcdef ABCDEF 00 11 00 11')
---
- MTEgMDAgMTEgMDAgYWJjZGVmIEFCQ0RFRiAwMCAxMSAwMCAxMQ==
...
digest.base64_decode('MTEgMDAgMTEgMDAgYWJjZGVmIEFCQ0RFRiAwMCAxMSAwMCAxMQ==')
---
- 11 00 11 00 abcdef ABCDEF 00 11 00 11
...
digest.base64_decode(nil)
---
-
...
digest.base64_encode(nil)
---
- error: 'builtin/digest.lua:76: Usage: base64.encode(str)'
...
digest.base64_encode(123)
---
-
...
digest.base64_decode(123)
---
-
...
digest = nil
---
...
......@@ -43,4 +43,16 @@ digest.crc32_update(4294967295, 'abc')
digest.crc32('abccde')
digest.crc32_update(digest.crc32('abc'), 'cde')
digest.base64_encode('12345')
digest.base64_decode('MTIzNDU=')
digest.base64_encode('asdfl asdf adfa zxc vzxcvz llll')
digest.base64_decode('YXNkZmwgYXNkZiBhZGZhIHp4YyB2enhjdnogbGxsbA==')
digest.base64_encode('11 00 11 00 abcdef ABCDEF 00 11 00 11')
digest.base64_decode('MTEgMDAgMTEgMDAgYWJjZGVmIEFCQ0RFRiAwMCAxMSAwMCAxMQ==')
digest.base64_decode(nil)
digest.base64_encode(nil)
digest.base64_encode(123)
digest.base64_decode(123)
digest = nil
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