]> foleosoft.com Git - CryptoFoleo.git/commitdiff
Thu Aug 10 01:06:33 PM EDT 2023
authorserver <[email protected]>
Thu, 10 Aug 2023 17:06:33 +0000 (13:06 -0400)
committerserver <[email protected]>
Thu, 10 Aug 2023 17:06:33 +0000 (13:06 -0400)
bin/CryptoFoleo.hi [new file with mode: 0644]
bin/CryptoFoleo.hs [new file with mode: 0644]
bin/CryptoFoleo.o [new file with mode: 0644]
bin/Main.hi
bin/Main.hs
bin/Main.o
bin/main
bin/prv.key [new file with mode: 0644]
bin/pub.key [new file with mode: 0644]

diff --git a/bin/CryptoFoleo.hi b/bin/CryptoFoleo.hi
new file mode 100644 (file)
index 0000000..bfdcf98
Binary files /dev/null and b/bin/CryptoFoleo.hi differ
diff --git a/bin/CryptoFoleo.hs b/bin/CryptoFoleo.hs
new file mode 100644 (file)
index 0000000..7459bae
--- /dev/null
@@ -0,0 +1,83 @@
+module CryptoFoleo
+    (
+        rsa_keygen,
+        rsa_import,
+        rsa_export
+    )
+where
+import System.IO as IO
+import Foreign
+import Foreign.C.Types
+import Foreign.C.String
+import Data.ByteString
+import Data.ByteString.Internal
+import Data.Word
+import Data.ByteString.Unsafe
+import Control.DeepSeq
+import qualified Data.ByteString.Internal as BI
+import qualified Foreign.Marshal.Utils as MU
+import qualified Data.ByteString.Char8 as C8
+
+foreign import ccall unsafe "foleo_rsa_keysize"
+    c_rsa_keysize :: IO (CUShort)
+
+foreign import ccall unsafe "foleo_rsa_keygen"
+    c_rsa_keygen :: CUShort -> Ptr () -> Ptr () -> IO ()
+
+foreign import ccall unsafe "foleo_rsa_export"
+    c_rsa_export :: Ptr () -> IO (Ptr (CUChar))
+
+foreign import ccall unsafe "foleo_rsa_import"
+    c_rsa_import :: Ptr (CUChar) -> Ptr () -> IO ()
+
+foreign import ccall unsafe "foleo_rsa_free"
+    c_rsa_free :: Ptr () -> IO ()
+
+foreign import ccall unsafe "free"
+    c_free :: Ptr a -> IO ()
+
+rsa_keygen :: Word16 -> (ByteString -> ByteString -> IO ()) -> IO ()
+rsa_keygen n fn = do
+    sRsaSize <- c_rsa_keysize
+    let rsaSize :: Int
+        rsaSize = fromIntegral sRsaSize
+    allocaBytes rsaSize $ \pubKeyPtr ->
+        allocaBytes rsaSize $ \prvKeyPtr -> do
+            c_rsa_keygen (fromIntegral n) pubKeyPtr prvKeyPtr
+            pubKey <- BI.create rsaSize (\ptr -> MU.copyBytes ptr (castPtr pubKeyPtr) rsaSize)
+            prvKey <- BI.create rsaSize (\ptr -> MU.copyBytes ptr (castPtr prvKeyPtr) rsaSize)
+            fn pubKey prvKey
+            rsa_free pubKey
+            rsa_free prvKey
+
+rsa_import :: String -> (ByteString -> IO ()) -> IO ()
+rsa_import n fn = do
+    sRsaSize <- c_rsa_keysize
+    let rsaSize :: Int
+        rsaSize = fromIntegral sRsaSize
+    unsafeUseAsCStringLen (C8.pack n) $ \(bsPtr, len) -> do
+        allocaBytes rsaSize $ \keyPtr -> do
+            c_rsa_import (castPtr bsPtr) keyPtr
+            key <- BI.create rsaSize(\ptr -> MU.copyBytes ptr (castPtr keyPtr) rsaSize)
+            fn key
+            rsa_free key
+
+rsa_export :: ByteString -> IO(String)
+rsa_export blob = do
+    useAsCString blob $ \blobPtr -> do
+        cStrPtr <- c_rsa_export (castPtr blobPtr)
+        cStr <- peekCString (castPtr cStrPtr)
+        c_free cStrPtr
+        return cStr
+
+rsa_free :: ByteString -> IO()
+rsa_free blob = useAsCString blob $ \ptr -> c_rsa_free (castPtr ptr)
+
+--main :: IO()
+--main = do
+--    rsa_keygen 2048 $ \pub prv -> do
+--        spub <- rsa_export pub
+--        IO.putStr spub
+
+byteStringToInteger :: ByteString -> Integer
+byteStringToInteger = foldl' (\acc byte -> acc * 256 + fromIntegral byte) 0
diff --git a/bin/CryptoFoleo.o b/bin/CryptoFoleo.o
new file mode 100644 (file)
index 0000000..53f1f2b
Binary files /dev/null and b/bin/CryptoFoleo.o differ
index aee81498a6b08f5117656b8c5ccb92f47dceb369..160f954aa4c4569bbb236dbb623e98b359638470 100644 (file)
Binary files a/bin/Main.hi and b/bin/Main.hi differ
index f4236ba70262e78277244130f8b84866c417cf99..67d63d5319c009b7513a60b6dcd7d21eb433198d 100644 (file)
@@ -1,60 +1,22 @@
 import System.IO as IO
-import Foreign
-import Foreign.C.Types
-import Foreign.C.String
-import Data.ByteString
-import Data.ByteString.Internal
-import Data.Word
-import Data.ByteString.Unsafe
-import Control.DeepSeq
-import qualified Data.ByteString.Internal as BI
-import qualified Foreign.Marshal.Utils as MU
-import qualified Data.ByteString.Char8 as C8
-
-foreign import ccall unsafe "foleo_rsa_keysize"
-    c_rsa_keysize :: IO(CUShort)
-
-foreign import ccall unsafe "foleo_rsa_keygen"
-    c_rsa_keygen :: CUShort -> Ptr() -> Ptr() -> IO()
-
-foreign import ccall unsafe "foleo_rsa_export"
-    c_rsa_export :: Ptr () -> IO (Ptr CUChar)
-
-foreign import ccall unsafe "foleo_rsa_free"
-    c_rsa_free :: Ptr () -> IO ()
-foreign import ccall unsafe "free"
-    c_free :: Ptr a -> IO ()
-
-rsa_keygen :: Word16 -> (ByteString -> ByteString -> IO()) -> IO()
-rsa_keygen n fn = do
-    sRsaSize <- c_rsa_keysize
-    let rsaSize :: Int
-        rsaSize = fromIntegral sRsaSize
-    allocaBytes rsaSize $ \pubKeyPtr ->
-        allocaBytes rsaSize $ \prvKeyPtr -> do
-            c_rsa_keygen (fromIntegral n) pubKeyPtr prvKeyPtr
-            pubKey <- BI.create rsaSize (\ptr -> MU.copyBytes ptr (castPtr pubKeyPtr) rsaSize)
-            prvKey <- BI.create rsaSize (\ptr -> MU.copyBytes ptr (castPtr prvKeyPtr) rsaSize)
-            fn pubKey prvKey
-            rsa_free pubKey
-            rsa_free prvKey
-
-rsa_export :: ByteString -> IO(String)
-rsa_export blob = do
-    useAsCString blob $ \blobPtr -> do
-        cStrPtr <- c_rsa_export (castPtr blobPtr)
-        cStr <- peekCString (castPtr cStrPtr)
-        c_free cStrPtr
-        return cStr
-
-rsa_free :: ByteString -> IO()
-rsa_free blob = useAsCString blob $ \ptr -> c_rsa_free (castPtr ptr)
+import System.Directory
+import Control.Monad
+import CryptoFoleo
 
 main :: IO()
 main = do
-    rsa_keygen 2048 $ \pub prv -> do
-        spub <- rsa_export pub
-        IO.putStr spub
+    existsPub <- doesFileExist "pub.key"
+    existsPrv <- doesFileExist "prv.key"
+    unless (existsPub && existsPrv) $ do
+        rsa_keygen 2048 $ \pub prv -> do
+            spub <- rsa_export pub
+            sprv <- rsa_export prv
+            writeFile "pub.key" spub
+            writeFile "prv.key" sprv
+
+    spub <- readFile "pub.key"
+    rsa_import spub $ \pubKey -> do
+        sspub <- rsa_export pubKey
+        putStrLn sspub
+    putStrLn spub
 
-byteStringToInteger :: ByteString -> Integer
-byteStringToInteger = foldl' (\acc byte -> acc * 256 + fromIntegral byte) 0
index 931635fa1abeed3b11da8f0551eae0768d074bdd..f2d221f4b4a09c16b02cc795805bb4b4d284f2ab 100644 (file)
Binary files a/bin/Main.o and b/bin/Main.o differ
index 895a2efb348d230f615fd68ee8d08cf6611f6cde..b57ff40b5fc655ab57e4b33f373e6de7ec2cf424 100755 (executable)
Binary files a/bin/main and b/bin/main differ
diff --git a/bin/prv.key b/bin/prv.key
new file mode 100644 (file)
index 0000000..bffcf44
--- /dev/null
@@ -0,0 +1,28 @@
++-------[RSA Private Key]--------+
+|#.[/[@$%#,\~%=@<[<(^,(&$:~'}<}``|
+|%$>(;&&%'($;')`#e~"},<<)@),^}%"<|
+|.%(?`]._*?@!e/@[*:)]/ex*},>~[s/(|
+|#;}#`*{=";[\,(=>.${x,:>!x(\"=]=&|
+|"%){"&x#>~}e]{^~\s'`'}><xx];s!@}|
+|<;_#,xx#?@#(}@\#`#:$!e}(:\#~#@>"|
+|$^]~%!`^~".\@<<{,@#<&;x")}:]{'^~|
+|`!@).{%.{}>e!/s:>`_es^?=(/}<]_*=|
+|]{@(s>.&\!`"$x@!{]>==s`.`$@{@`/<|
+|^??^/}^*['_<:@(^?]/:`~[[={*[_@#]|
+|::]}*@!)?}/`]=`/e#\';#(}[!,&<!e_|
+|\}>:"`>!{'x!*./!,x;_%{^<]:<)$""^|
+|;x[[^&!?e?=__']s~<?x*\#)_s $`,^_|
+|=^`s\e#^e,>.,`,$&!?`:)/{\}!&!\{/|
+|`#:`)x%)"e`?,[s.)e=>>^`(;,$}'(<:|
+|%."{(=_,%^!ex,$,_x>*`%^,ex;]x_$;|
+|!.!.#.x]e/}*~;<,s/{~?e`@}!~$)e_.|
+|;,s),s@.{$ex{@=<s?\_/]/=*(\`'%=`|
+|!/~?][](.=x="'&^{s`(,?$}))&;)(x;|
+|{[(;`>;];:{/(}$^.%(&;%\x!{}<,x!{|
+|!<={'/}x?^'.s&;$(*~*")ses`},!:;*|
+|e;s&~.:`;xx&_,}[};x@e=){]s&%'[_:|
+|%!!\_%{(!`(;^/_:<^?@,=@%)>,/$)<s|
+|}`!#*~/{`'*"/[?$_]"?#?:s'}*=>@,_|
+|[.~!<\!<&.`%}^=s/?@_x=":'{~[_={)|
+|\*$~~?.#};"`\[)[\[~;s           |
++--------------------------------+
diff --git a/bin/pub.key b/bin/pub.key
new file mode 100644 (file)
index 0000000..6b608b5
--- /dev/null
@@ -0,0 +1,15 @@
++--------[RSA Public Key]--------+
+|(^:/# $`,^_=^`s\e#^e,>.,`,$&!?`:|
+|)/{\}!&!\{/`#:`)x%)"e`?,[s.)e=>>|
+|^`(;,$}'(<:%."{(=_,%^!ex,$,_x>*`|
+|%^,ex;]x_$;!.!.#.x]e/}*~;<,s/{~?|
+|e`@}!~$)e_.;,s),s@.{$ex{@=<s?\_/|
+|]/=*(\`'%=`!/~?][](.=x="'&^{s`(,|
+|?$}))&;)(x;{[(;`>;];:{/(}$^.%(&;|
+|%\x!{}<,x!{!<={'/}x?^'.s&;$(*~*"|
+|)ses`},!:;*e;s&~.:`;xx&_,}[};x@e|
+|=){]s&%'[_:%!!\_%{(!`(;^/_:<^?@,|
+|=@%)>,/$)<s}`!#*~/{`'*"/[?$_]"?#|
+|?:s'}*=>@,_[.~!<\!<&.`%}^=s/?@_x|
+|=":'{~[_={)\*$~~?.#};"`\[)[\[~;s|
++--------------------------------+