git.iko.soy/typeable/entropy
on main help sign in
Merge pull request #47 from eskimor/ghcjs

Added ghcjs support.

commitee660d9dd29120f7def2c813112403269ffce8eb authorThomas M. DuBuisson <thomas.dubuisson@gmail.com> dateThu 21 Mar 2019, 16:50 (8 years ago) parent419a4cb2cff147
3 files changed+99−31
System/Entropy.hs+7−5 System/EntropyGhcjs.hs+51 entropy.cabal+41−26
System/Entropy.hs+7−5
@@ -12,6 +12,7 @@
1212 - *nix systems via @\/dev\/urandom@
1313 - Includes QNX
1414 - Xen (only when RDRAND is available)
15+ - ghcjs/browser via JavaScript crypto API.
1516-}
1617
1718module System.Entropy
@@ -22,21 +23,22 @@ module System.Entropy
2223 hGetEntropy,
2324 closeHandle
2425 ) where
25−#if defined(isWindows)
26+
27+#ifdef ghcjs_HOST_OS
28+import System.EntropyGhcjs
29+#elif defined(isWindows)
2630import System.EntropyWindows
27−#else
28−#ifdef XEN
31+#elif XEN
2932import System.EntropyXen
3033#else
3134import System.EntropyNix
3235#endif
33−#endif
3436
3537import qualified Data.ByteString as B
3638import Control.Exception (bracket)
3739
3840-- |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.
4042-- (As of 0.4. Verions <0.4 mixed system and hardware sources)
4143--
4244-- 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
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,51 @@ 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 >= 2.5
70− cpp-options: -DisWindows
71− cc-options: -DisWindows
72− extra-libraries: advapi32
73− else
74− if !os(halvm)
75− Build-Depends: unix
63+
64+ if impl(ghcjs) {
65+ build-depends: ghcjs-dom
66+ , jsaddle
67+ }
68+ else {
69+ if(os(halvm))
70+ cpp-options: -DXEN -DHAVE_RDRAND
71+ cc-options: -DXEN -DHAVE_RDRAND
72+ if arch(x86_64)
73+ cpp-options: -Darch_x86_64
74+ cc-options: -Darch_x86_64 -O2
75+ -- gcc 4.8.2 on i386 fails to compile rdrand.c when using -fPIC!
76+ c-sources: cbits/rdrand.c
77+ include-dirs: cbits
78+ if arch(i386)
79+ cpp-options: -Darch_i386
80+ cc-options: -Darch_i386 -O2
81+ if os(windows)
82+ build-depends: Win32 >= 2.5
83+ cpp-options: -DisWindows
84+ cc-options: -DisWindows
85+ extra-libraries: advapi32
86+ else
87+ if !os(halvm)
88+ Build-Depends: unix
89+ }
90+
7691
7792source-repository head
7893 type: git