Skip to content
Snippets Groups Projects
Commit 6b2ca8c8 authored by Roman Tsisyk's avatar Roman Tsisyk
Browse files

Merge branch 'gh-673-random'

parents 89bc809c e1d92bd2
No related branches found
No related tags found
No related merge requests found
......@@ -11,6 +11,7 @@
#include "lua/bsdsocket.h"
#include "lua/digest.h"
#include "base64.h"
#include "random.h"
#include <lib/salad/guava.h>
/*
......@@ -52,5 +53,6 @@ void *ffi_symbols[] = {
(void *) base64_encode,
(void *) base64_bufsize,
(void *) SHA1internal,
(void *) guava
(void *) guava,
(void *) random_bytes,
};
......@@ -30,6 +30,9 @@ ffi.cdef[[
int base64_bufsize(int binsize);
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);
/* random */
void random_bytes(char *, size_t);
]]
local ssl
......@@ -135,6 +138,15 @@ local m = {
guava = function(state, buckets)
return ffi.C.guava(state, buckets)
end,
urandom = function(n)
if n == nil then
error('Usage: digest.urandom(len)')
end
local buf = ffi.new('char[?]', n)
ffi.C.random_bytes(buf, n)
return ffi.string(buf, n)
end
}
if ssl ~= nil then
......
......@@ -193,19 +193,19 @@ digest.base64_decode(b) == s
...
digest.base64_decode(nil)
---
- error: 'builtin/digest.lua:88: Usage: digest.base64_decode(string)'
- error: 'builtin/digest.lua:91: Usage: digest.base64_decode(string)'
...
digest.base64_encode(nil)
---
- error: 'builtin/digest.lua:77: Usage: digest.base64_encode(string)'
- error: 'builtin/digest.lua:80: Usage: digest.base64_encode(string)'
...
digest.base64_encode(123)
---
- error: 'builtin/digest.lua:77: Usage: digest.base64_encode(string)'
- error: 'builtin/digest.lua:80: Usage: digest.base64_encode(string)'
...
digest.base64_decode(123)
---
- error: 'builtin/digest.lua:88: Usage: digest.base64_decode(string)'
- error: 'builtin/digest.lua:91: Usage: digest.base64_decode(string)'
...
digest.guava('hello', 0)
---
......@@ -227,6 +227,22 @@ digest.guava(1673758223894951030, 11)
---
- 7
...
digest.urandom()
---
- error: 'builtin/digest.lua:144: Usage: digest.urandom(len)'
...
#digest.urandom(0)
---
- 0
...
#digest.urandom(1)
---
- 1
...
#digest.urandom(16)
---
- 16
...
digest = nil
---
...
......@@ -65,4 +65,9 @@ digest.guava(10863919174838991, 11)
digest.guava(2016238256797177309, 11)
digest.guava(1673758223894951030, 11)
digest.urandom()
#digest.urandom(0)
#digest.urandom(1)
#digest.urandom(16)
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