factor/basis/random/windows/windows.factor

53 lines
1.5 KiB
Factor
Raw Normal View History

2008-03-28 23:10:01 -04:00
USING: accessors alien.c-types byte-arrays continuations
kernel windows windows.advapi32 init namespaces random
destructors locals ;
2008-03-19 17:18:03 -04:00
IN: random.windows
TUPLE: windows-rng provider type ;
C: <windows-rng> windows-rng
2008-03-28 23:10:01 -04:00
TUPLE: windows-crypto-context handle ;
2008-03-28 23:10:01 -04:00
C: <windows-crypto-context> windows-crypto-context
M: windows-crypto-context dispose ( tuple -- )
handle>> 0 CryptReleaseContext win32-error=0/f ;
: factor-crypto-container ( -- string ) "FactorCryptoContainer" ; inline
:: (acquire-crypto-context) ( provider type flags -- handle )
[let | handle [ "HCRYPTPROV" <c-object> ] |
handle
factor-crypto-container
provider
type
flags
CryptAcquireContextW win32-error=0/f
handle *void* ] ;
: acquire-crypto-context ( provider type -- handle )
[ 0 (acquire-crypto-context) ]
[ drop CRYPT_NEWKEYSET (acquire-crypto-context) ] recover ;
: windows-crypto-context ( provider type -- context )
acquire-crypto-context <windows-crypto-context> ;
M: windows-rng random-bytes* ( n tuple -- bytes )
[
[ provider>> ] [ type>> ] bi
2008-05-14 20:03:07 -04:00
windows-crypto-context &dispose
handle>> swap dup <byte-array>
[ CryptGenRandom win32-error=0/f ] keep
] with-destructors ;
[
MS_DEF_PROV
PROV_RSA_FULL <windows-rng> system-random-generator set-global
2008-03-27 18:43:00 -04:00
MS_STRONG_PROV
PROV_RSA_FULL <windows-rng> secure-random-generator set-global
2008-03-27 18:43:00 -04:00
! MS_ENH_RSA_AES_PROV
! PROV_RSA_AES <windows-rng> secure-random-generator set-global
] "random.windows" add-init-hook