git.iko.soy/typeable/entropy
on main help sign in
Added ghcjs support.
commit320f96e33f322e28f597a244bd4736b9ca5ca4e5 authorRobert Klotzner <robert.klotzner@gmx.at> dateFri 15 Mar 2019, 14:04 (8 years ago) parentf699158
3 files changed+96−31
System/Entropy.hs+6−5 System/EntropyGhcjs.hs+51 entropy.cabal+39−26
System/Entropy.hs+6−5
@@ -22,21 +22,22 @@ module System.Entropy
2222 hGetEntropy,
2323 closeHandle
2424 ) where
25−#if defined(isWindows)
25+
26+#ifdef ghcjs_HOST_OS
27+import System.EntropyGhcjs
28+#elif defined(isWindows)
2629import System.EntropyWindows
27−#else
28−#ifdef XEN
30+#elif XEN
2931import System.EntropyXen
3032#else
3133import System.EntropyNix
3234#endif
33−#endif
3435
3536import qualified Data.ByteString as B
3637import Control.Exception (bracket)
3738
3839-- |Get a specific number of bytes of cryptographically
39−-- secure random data using the *system-specific* sources.
40+-- secure random data using the *system-specific* sources.
4041-- (As of 0.4. Verions <0.4 mixed system and hardware sources)
4142--
4243-- 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+39−26
@@ -14,9 +14,11 @@ category: Data, Cryptography
1414homepage: https://github.com/TomMD/entropy
1515bug-reports: https://github.com/TomMD/entropy/issues
1616stability: stable
17+
1718-- build-type: Simple
1819-- ^^ Used for HaLVM
1920build-type: Custom
21+
2022-- ^^ Test for RDRAND support using 'ghc'
2123cabal-version: >=1.10
2224tested-with: GHC == 8.2.2
@@ -41,38 +43,49 @@ custom-setup
4143library
4244 ghc-options: -O2
4345 exposed-modules: System.Entropy
44− if os(windows)
45− other-modules: System.EntropyWindows
46+ if impl(ghcjs)
47+ other-modules: System.EntropyGhcjs
4648 else {
47− if os(halvm)
48− other-modules: System.EntropyXen
49− else
50− other-modules: System.EntropyNix
49+ if os(windows)
50+ other-modules: System.EntropyWindows
51+ else {
52+ if os(halvm)
53+ other-modules: System.EntropyXen
54+ else
55+ other-modules: System.EntropyNix
56+ }
5157 }
5258 other-extensions: CPP, ForeignFunctionInterface, BangPatterns,
5359 ScopedTypeVariables
5460 build-depends: base >= 4.8 && < 5, bytestring
61+
5562 default-language: Haskell2010
56− if(os(halvm))
57− cpp-options: -DXEN -DHAVE_RDRAND
58− cc-options: -DXEN -DHAVE_RDRAND
59− if arch(x86_64)
60− cpp-options: -Darch_x86_64
61− cc-options: -Darch_x86_64 -O2
62− -- gcc 4.8.2 on i386 fails to compile rdrand.c when using -fPIC!
63− c-sources: cbits/rdrand.c
64− include-dirs: cbits
65− if arch(i386)
66− cpp-options: -Darch_i386
67− cc-options: -Darch_i386 -O2
68− if os(windows)
69− build-depends: Win32
70− cpp-options: -DisWindows
71− cc-options: -DisWindows
72− extra-libraries: advapi32
73− else
74− if !os(halvm)
75− Build-Depends: unix
63+ if impl(ghcjs) {
64+ build-depends: ghcjs-dom
65+ , jsaddle
66+ }
67+ else {
68+ if(os(halvm))
69+ cpp-options: -DXEN -DHAVE_RDRAND
70+ cc-options: -DXEN -DHAVE_RDRAND
71+ if arch(x86_64)
72+ cpp-options: -Darch_x86_64
73+ cc-options: -Darch_x86_64 -O2
74+ -- gcc 4.8.2 on i386 fails to compile rdrand.c when using -fPIC!
75+ c-sources: cbits/rdrand.c
76+ include-dirs: cbits
77+ if arch(i386)
78+ cpp-options: -Darch_i386
79+ cc-options: -Darch_i386 -O2
80+ if os(windows)
81+ build-depends: Win32
82+ cpp-options: -DisWindows
83+ cc-options: -DisWindows
84+ extra-libraries: advapi32
85+ else
86+ if !os(halvm)
87+ Build-Depends: unix
88+ }
7689
7790source-repository head
7891 type: git