File tree Expand file tree Collapse file tree 3 files changed +46
-2
lines changed Expand file tree Collapse file tree 3 files changed +46
-2
lines changed Original file line number Diff line number Diff line change @@ -27,6 +27,8 @@ module System.Entropy
27
27
import System.EntropyGhcjs
28
28
#elif defined(isWindows)
29
29
import System.EntropyWindows
30
+ #elif wasi_HOST_OS
31
+ import System.EntropyWasi
30
32
#else
31
33
import System.EntropyNix
32
34
#endif
Original file line number Diff line number Diff line change
1
+ {-# LANGUAGE CApiFFI #-}
2
+
3
+ module System.EntropyWasi where
4
+
5
+ import Control.Monad (when )
6
+ import Data.ByteString (ByteString )
7
+ import Data.ByteString.Internal as B
8
+ import Data.Word (Word8 )
9
+ import Foreign.C.Types
10
+ import Foreign.Ptr
11
+
12
+ data CryptHandle = BogusCryptHandle
13
+
14
+ openHandle :: IO CryptHandle
15
+ openHandle = pure BogusCryptHandle
16
+
17
+ hGetEntropy :: CryptHandle -> Int -> IO ByteString
18
+ hGetEntropy BogusCryptHandle = \ n -> B. create n $ go (fromIntegral n)
19
+ where
20
+ go :: CSize -> Ptr Word8 -> IO ()
21
+ go n ptr
22
+ | n <= 256 = getentropy' ptr n
23
+ | otherwise = do
24
+ getentropy' ptr 256
25
+ go (n - 256 ) (ptr `plusPtr` 256 )
26
+
27
+ getentropy' ptr n = do
28
+ res <- getentropy ptr n
29
+ when (res /= 0 ) $
30
+ fail " getentropy failed"
31
+
32
+ foreign import capi safe " unistd.h getentropy"
33
+ getentropy :: Ptr Word8 -> CSize -> IO CInt
34
+
35
+ closeHandle :: CryptHandle -> IO ()
36
+ closeHandle BogusCryptHandle = pure ()
37
+
38
+ hardwareRandom :: Int -> IO (Maybe ByteString )
39
+ hardwareRandom _ = pure Nothing
Original file line number Diff line number Diff line change 1
- cabal-version : >= 1.10
1
+ cabal-version : 3.8
2
2
name : entropy
3
3
version : 0.4.1.10
4
4
x-revision : 2
@@ -7,7 +7,7 @@ description: A mostly platform independent method to obtain cryptographically
7
7
Users looking for cryptographically strong (number-theoretically
8
8
sound) PRNGs should see the 'DRBG' package too.
9
9
synopsis : A platform independent entropy source
10
- license : BSD3
10
+ license : BSD-3-Clause
11
11
license-file : LICENSE
12
12
copyright : Thomas DuBuisson <
[email protected] >
13
13
author : Thomas DuBuisson <
[email protected] >
@@ -61,6 +61,8 @@ library
61
61
else {
62
62
if os(windows)
63
63
other-modules : System.EntropyWindows
64
+ elif os(wasi)
65
+ other-modules : System.EntropyWasi
64
66
else {
65
67
other-modules : System.EntropyNix
66
68
}
@@ -90,6 +92,7 @@ library
90
92
cpp-options : -DisWindows
91
93
cc-options : -DisWindows
92
94
extra-libraries : advapi32
95
+ elif os(wasi)
93
96
else
94
97
Build-Depends : unix
95
98
c-sources : cbits/getrandom.c cbits/random_initialized.c
You can’t perform that action at this time.
0 commit comments