fix random
add with-secure-random
parent
4feebaa8e8
commit
37cffc50fa
|
@ -0,0 +1,24 @@
|
|||
! Copyright (C) 2008 Doug Coleman.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: alien.c-types kernel math namespaces sequences
|
||||
io.backend io.binary combinators system vocabs.loader ;
|
||||
IN: random.backend
|
||||
|
||||
SYMBOL: insecure-random-generator
|
||||
SYMBOL: secure-random-generator
|
||||
SYMBOL: random-generator
|
||||
|
||||
GENERIC: seed-random ( tuple seed -- )
|
||||
GENERIC: random-32* ( tuple -- r )
|
||||
GENERIC: random-bytes* ( n tuple -- bytes )
|
||||
|
||||
M: object random-bytes* ( n tuple -- byte-array )
|
||||
swap [ drop random-32* ] with map >c-uint-array ;
|
||||
|
||||
M: object random-32* ( tuple -- n ) 4 random-bytes* le> ;
|
||||
|
||||
ERROR: no-random-number-generator ;
|
||||
|
||||
M: f random-bytes* ( n obj -- * ) no-random-number-generator ;
|
||||
|
||||
M: f random-32* ( obj -- * ) no-random-number-generator ;
|
|
@ -1,4 +1,4 @@
|
|||
USING: kernel random math accessors ;
|
||||
USING: kernel random math accessors random.backend ;
|
||||
IN: random.dummy
|
||||
|
||||
TUPLE: random-dummy i ;
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
USING: kernel math random namespaces random.mersenne-twister
|
||||
sequences tools.test ;
|
||||
sequences tools.test random.backend ;
|
||||
IN: random.mersenne-twister.tests
|
||||
|
||||
: check-random ( max -- ? )
|
||||
|
|
|
@ -4,7 +4,8 @@
|
|||
! http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/CODES/mt19937ar.c
|
||||
|
||||
USING: arrays kernel math namespaces sequences system init
|
||||
accessors math.ranges combinators.cleave random new-effects ;
|
||||
accessors math.ranges combinators.cleave random new-effects
|
||||
random.backend ;
|
||||
IN: random.mersenne-twister
|
||||
|
||||
<PRIVATE
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
USING: random sequences tools.test ;
|
||||
IN: random.tests
|
||||
|
||||
[ 4 ] [ 4 random-bytes length ] unit-test
|
||||
[ 7 ] [ 7 random-bytes length ] unit-test
|
|
@ -1,30 +1,15 @@
|
|||
! Copyright (C) 2008 Doug Coleman.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: alien.c-types kernel math namespaces sequences
|
||||
io.backend io.binary ;
|
||||
io.backend io.binary combinators system vocabs.loader
|
||||
random.backend random.mersenne-twister init ;
|
||||
USE: prettyprint
|
||||
IN: random
|
||||
|
||||
SYMBOL: random-generator
|
||||
|
||||
HOOK: os-crypto-random-bytes io-backend ( n -- byte-array )
|
||||
HOOK: os-random-bytes io-backend ( n -- byte-array )
|
||||
HOOK: os-crypto-random-32 io-backend ( -- r )
|
||||
HOOK: os-random-32 io-backend ( -- r )
|
||||
|
||||
GENERIC: seed-random ( tuple seed -- )
|
||||
GENERIC: random-32* ( tuple -- r )
|
||||
GENERIC: random-bytes* ( tuple n -- bytes )
|
||||
|
||||
M: object random-bytes* ( tuple n -- byte-array )
|
||||
[ drop random-32* ] with map >c-uint-array ;
|
||||
|
||||
M: object random-32* ( tuple -- n )
|
||||
4 random-bytes* le> ;
|
||||
|
||||
: random-bytes ( n -- r )
|
||||
[
|
||||
4 /mod zero? [ 1+ ] unless
|
||||
random-generator get swap random-bytes*
|
||||
dup 4 rem zero? [ 1+ ] unless
|
||||
random-generator get random-bytes*
|
||||
] keep head ;
|
||||
|
||||
: random ( seq -- elt )
|
||||
|
@ -41,3 +26,16 @@ M: object random-32* ( tuple -- n )
|
|||
|
||||
: with-random ( tuple quot -- )
|
||||
random-generator swap with-variable ; inline
|
||||
|
||||
: with-secure-random ( quot -- )
|
||||
>r secure-random-generator get r> with-random ; inline
|
||||
|
||||
{
|
||||
{ [ windows? ] [ "random.windows" require ] }
|
||||
{ [ unix? ] [ "random.unix" require ] }
|
||||
} cond
|
||||
|
||||
[
|
||||
[ 32 random-bits ] with-secure-random
|
||||
<mersenne-twister> random-generator set-global
|
||||
] "random" add-init-hook
|
||||
|
|
|
@ -1,22 +1,21 @@
|
|||
USING: alien.c-types io io.files io.nonblocking kernel
|
||||
namespaces random io.encodings.binary singleton ;
|
||||
namespaces random.backend io.encodings.binary singleton init
|
||||
accessors ;
|
||||
IN: random.unix
|
||||
|
||||
SINGLETON: unix-random
|
||||
TUPLE: unix-random path ;
|
||||
|
||||
C: <unix-random> unix-random
|
||||
|
||||
: file-read-unbuffered ( n path -- bytes )
|
||||
over default-buffer-size [
|
||||
binary <file-reader> [ read ] with-stream
|
||||
] with-variable ;
|
||||
|
||||
M: unix-random os-crypto-random-bytes ( n -- byte-array )
|
||||
"/dev/random" file-read-unbuffered ;
|
||||
M: unix-random random-bytes* ( n tuple -- byte-array )
|
||||
path>> file-read-unbuffered ;
|
||||
|
||||
M: unix-random os-random-bytes ( n -- byte-array )
|
||||
"/dev/urandom" file-read-unbuffered ;
|
||||
|
||||
M: unix-random os-crypto-random-32 ( -- r )
|
||||
4 os-crypto-random-bytes *uint ;
|
||||
|
||||
M: unix-random os-random-32 ( -- r )
|
||||
4 os-random-bytes *uint ;
|
||||
[
|
||||
"/dev/random" <unix-random> secure-random-generator set-global
|
||||
"/dev/urandom" <unix-random> insecure-random-generator set-global
|
||||
] "random.unix" add-init-hook
|
||||
|
|
|
@ -1,28 +0,0 @@
|
|||
USING: accessors alien.c-types byte-arrays continuations
|
||||
kernel random windows windows.advapi32 ;
|
||||
IN: random.windows.cryptographic
|
||||
|
||||
TUPLE: windows-crypto-context handle ;
|
||||
|
||||
C: <windows-crypto-context> windows-crypto-context
|
||||
|
||||
M: windows-crypto-context dispose ( tuple -- )
|
||||
handle>> 0 CryptReleaseContext win32-error=0/f ;
|
||||
|
||||
|
||||
TUPLE: windows-cryptographic-rng context ;
|
||||
|
||||
C: <windows-cryptographic-rng> windows-cryptographic-rng
|
||||
|
||||
M: windows-cryptographic-rng dispose ( tuple -- )
|
||||
context>> dispose ;
|
||||
|
||||
M: windows-cryptographic-rng random-bytes* ( tuple n -- bytes )
|
||||
>r context>> r> dup <byte-array>
|
||||
[ CryptGenRandom win32-error=0/f ] keep ;
|
||||
|
||||
: acquire-aes-context ( -- bytes )
|
||||
"HCRYPTPROV" <c-object>
|
||||
dup f f PROV_RSA_AES CRYPT_NEWKEYSET
|
||||
CryptAcquireContextW win32-error=0/f *void*
|
||||
<windows-crypto-context> ;
|
|
@ -1,3 +1,31 @@
|
|||
USING: accessors alien.c-types byte-arrays continuations
|
||||
kernel random windows windows.advapi32 init namespaces random ;
|
||||
IN: random.windows
|
||||
|
||||
! M: windows-io
|
||||
TUPLE: windows-crypto-context handle ;
|
||||
|
||||
C: <windows-crypto-context> windows-crypto-context
|
||||
|
||||
M: windows-crypto-context dispose ( tuple -- )
|
||||
handle>> 0 CryptReleaseContext win32-error=0/f ;
|
||||
|
||||
TUPLE: windows-cryptographic-rng context ;
|
||||
|
||||
C: <windows-cryptographic-rng> windows-cryptographic-rng
|
||||
|
||||
M: windows-cryptographic-rng dispose ( tuple -- )
|
||||
context>> dispose ;
|
||||
|
||||
M: windows-cryptographic-rng random-bytes* ( tuple n -- bytes )
|
||||
>r context>> r> dup <byte-array>
|
||||
[ CryptGenRandom win32-error=0/f ] keep ;
|
||||
|
||||
: windows-aes-context ( -- context )
|
||||
"HCRYPTPROV" <c-object>
|
||||
dup f f PROV_RSA_AES CRYPT_NEWKEYSET
|
||||
CryptAcquireContextW win32-error=0/f *void*
|
||||
<windows-crypto-context> ;
|
||||
|
||||
[
|
||||
windows-aes-context secure-random-generator set-global
|
||||
] "random.windows" add-init-hook
|
||||
|
|
Loading…
Reference in New Issue