git.iko.soy/typeable/entropy
on main help sign in
Merge pull request #47 from eskimor/ghcjs
ee660d9Thomas M. DuBuisson
System/Entropy.hs+7−5
@@ -12,6 +12,7 @@
12 - *nix systems via @\/dev\/urandom@
12 - *nix systems via @\/dev\/urandom@
13 - Includes QNX
13 - Includes QNX
14 - Xen (only when RDRAND is available)
14 - Xen (only when RDRAND is available)
15+ - ghcjs/browser via JavaScript crypto API.
15-}
16-}
16
17
17module System.Entropy
18module System.Entropy
@@ -22,21 +23,22 @@ module System.Entropy
22 hGetEntropy,
23 hGetEntropy,
23 closeHandle
24 closeHandle
24 ) where
25 ) where
26+
27+#ifdef ghcjs_HOST_OS
28+import System.EntropyGhcjs
25−#if defined(isWindows)
29+#elif defined(isWindows)
26import System.EntropyWindows
30import System.EntropyWindows
27−#else
28−#ifdef XEN
31+#elif XEN
29import System.EntropyXen
32import System.EntropyXen
30#else
33#else
31import System.EntropyNix
34import System.EntropyNix
32#endif
35#endif
33−#endif
34
36
35import qualified Data.ByteString as B
37import qualified Data.ByteString as B
36import Control.Exception (bracket)
38import Control.Exception (bracket)
37
39
38-- |Get a specific number of bytes of cryptographically
40-- |Get a specific number of bytes of cryptographically
39−-- secure random data using the *system-specific* sources.
41+-- secure random data using the *system-specific* sources.
40-- (As of 0.4. Verions <0.4 mixed system and hardware sources)
42-- (As of 0.4. Verions <0.4 mixed system and hardware sources)
41--
43--
42-- The returned random value is considered cryptographically secure but not true entropy.
44-- The returned random value is considered cryptographically secure but not true entropy.
System/EntropyGhcjs.hsnew file+51
@@ -0,0 +1,51 @@
1+{-|
2+ Maintainer: Thomas.DuBuisson@gmail.com
3+ Stability: beta
4+ Portability: portable
5+
6+ Obtain entropy from system sources or x86 RDRAND when available.
7+
8+-}
9+
10+module System.EntropyGhcjs
11+ ( CryptHandle
12+ , openHandle
13+ , hGetEntropy
14+ , closeHandle
15+ , hardwareRandom
16+ ) where
17+
18+import Data.ByteString as B
19+import GHCJS.DOM.Crypto as Crypto
20+import GHCJS.DOM.Types (ArrayBufferView (..), fromJSValUnchecked)
21+import GHCJS.DOM.GlobalCrypto (getCrypto)
22+import GHCJS.DOM (currentWindowUnchecked)
23+import Language.Javascript.JSaddle.Object as JS
24+
25+
26+-- |Handle for manual resource management
27+newtype CryptHandle = CH Crypto
28+
29+-- | Get random values from the hardward RNG or return Nothing if no
30+-- supported hardware RNG is available.
31+--
32+-- Not supported on ghcjs.
33+hardwareRandom :: Int -> IO (Maybe B.ByteString)
34+hardwareRandom _ = pure Nothing
35+
36+-- |Open a `CryptHandle`
37+openHandle :: IO CryptHandle
38+openHandle = do
39+ w <- currentWindowUnchecked
40+ CH <$> getCrypto w
41+
42+-- |Close the `CryptHandle`
43+closeHandle :: CryptHandle -> IO ()
44+closeHandle _ = pure ()
45+
46+-- |Read random data from a `CryptHandle`
47+hGetEntropy :: CryptHandle -> Int -> IO B.ByteString
48+hGetEntropy (CH h) n = do
49+ arr <- JS.new (jsg "Int8Array") [n]
50+ getRandomValues_ h (ArrayBufferView arr)
51+ B.pack <$> fromJSValUnchecked arr
entropy.cabal+41−26
@@ -14,9 +14,11 @@ category: Data, Cryptography
14homepage: https://github.com/TomMD/entropy
14homepage: https://github.com/TomMD/entropy
15bug-reports: https://github.com/TomMD/entropy/issues
15bug-reports: https://github.com/TomMD/entropy/issues
16stability: stable
16stability: stable
17+
17-- build-type: Simple
18-- build-type: Simple
18-- ^^ Used for HaLVM
19-- ^^ Used for HaLVM
19build-type: Custom
20build-type: Custom
21+
20-- ^^ Test for RDRAND support using 'ghc'
22-- ^^ Test for RDRAND support using 'ghc'
21cabal-version: >=1.10
23cabal-version: >=1.10
22tested-with: GHC == 8.2.2
24tested-with: GHC == 8.2.2
@@ -41,38 +43,51 @@ custom-setup
41library
43library
42 ghc-options: -O2
44 ghc-options: -O2
43 exposed-modules: System.Entropy
45 exposed-modules: System.Entropy
44− if os(windows)
46+ if impl(ghcjs)
45− other-modules: System.EntropyWindows
47+ other-modules: System.EntropyGhcjs
46 else {
48 else {
49+ if os(windows)
50+ other-modules: System.EntropyWindows
51+ else {
47− if os(halvm)
52+ if os(halvm)
48− other-modules: System.EntropyXen
53+ other-modules: System.EntropyXen
49− else
54+ else
50− other-modules: System.EntropyNix
55+ other-modules: System.EntropyNix
56+ }
51 }
57 }
52 other-extensions: CPP, ForeignFunctionInterface, BangPatterns,
58 other-extensions: CPP, ForeignFunctionInterface, BangPatterns,
53 ScopedTypeVariables
59 ScopedTypeVariables
54 build-depends: base >= 4.8 && < 5, bytestring
60 build-depends: base >= 4.8 && < 5, bytestring
61+
55 default-language: Haskell2010
62 default-language: Haskell2010
63+
64+ if impl(ghcjs) {
65+ build-depends: ghcjs-dom
66+ , jsaddle
67+ }
68+ else {
56− if(os(halvm))
69+ if(os(halvm))
57− cpp-options: -DXEN -DHAVE_RDRAND
70+ cpp-options: -DXEN -DHAVE_RDRAND
58− cc-options: -DXEN -DHAVE_RDRAND
71+ cc-options: -DXEN -DHAVE_RDRAND
59− if arch(x86_64)
72+ if arch(x86_64)
60− cpp-options: -Darch_x86_64
73+ cpp-options: -Darch_x86_64
61− cc-options: -Darch_x86_64 -O2
74+ cc-options: -Darch_x86_64 -O2
62− -- gcc 4.8.2 on i386 fails to compile rdrand.c when using -fPIC!
75+ -- gcc 4.8.2 on i386 fails to compile rdrand.c when using -fPIC!
63− c-sources: cbits/rdrand.c
76+ c-sources: cbits/rdrand.c
64− include-dirs: cbits
77+ include-dirs: cbits
65− if arch(i386)
78+ if arch(i386)
66− cpp-options: -Darch_i386
79+ cpp-options: -Darch_i386
67− cc-options: -Darch_i386 -O2
80+ cc-options: -Darch_i386 -O2
68− if os(windows)
81+ if os(windows)
69− build-depends: Win32 >= 2.5
82+ build-depends: Win32 >= 2.5
70− cpp-options: -DisWindows
83+ cpp-options: -DisWindows
71− cc-options: -DisWindows
84+ cc-options: -DisWindows
72− extra-libraries: advapi32
85+ extra-libraries: advapi32
73− else
86+ else
74− if !os(halvm)
87+ if !os(halvm)
75− Build-Depends: unix
88+ Build-Depends: unix
89+ }
90+
76
91
77source-repository head
92source-repository head
78 type: git
93 type: git