Commit 1ba1ac9e authored by Vicente Parra Hurtado's avatar Vicente Parra Hurtado

Alpha 0.2

parent 22e31ce8
This diff is collapsed.
......@@ -3,23 +3,19 @@ module EncrypText
,decryptext
) where
import RandomGenerator
import LayerAES256
import NukeBull
import System.Random
import Crypto.Cipher.AES (AES256)
import Crypto.Cipher.Types (BlockCipher(..), Cipher(..),IV, makeIV)
import Crypto.Error (throwCryptoError)
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as BC
encryptext :: (RandomGen g) => g -> Int -> B.ByteString -> B.ByteString -> String -> IO B.ByteString
encryptext :: (RandomGen g) => g -> Int -> B.ByteString -> B.ByteString -> B.ByteString -> IO B.ByteString
encryptext g num key vector msg = do
let text = entrophyBS num (BC.pack msg) g
let text = entrophyBS num msg g
let mInitIV = makeIV vector
case mInitIV of
Nothing -> error "Failed to generate and initialization vector."
......
module LayerAES256
(encryptAES256
,decryptAES256
) where
import Crypto.Cipher.AES (AES256)
import Crypto.Cipher.Types (BlockCipher(..), Cipher(..),IV, makeIV)
import Crypto.Error (throwCryptoError)
import qualified Data.ByteString as B
encryptAES256 :: B.ByteString -> IV AES256 -> B.ByteString -> B.ByteString
encryptAES256 key cIV plainData = cbcEncrypt ctx cIV plainData
where ctx :: AES256
ctx = throwCryptoError $ cipherInit key
decryptAES256 :: B.ByteString -> IV AES256 -> B.ByteString -> B.ByteString
decryptAES256 key cIV plainData = cbcDecrypt ctx cIV plainData
where ctx :: AES256
ctx = throwCryptoError $ cipherInit key
module RandomGenerator
(deriveKey
module NukeBull
(stringtoWord8
,word8toString
,randomString
,generator
,randomBytes
......@@ -8,6 +9,9 @@ module RandomGenerator
,extendW8
,entrophyBS
,entrophyW8
,encryptAES256
,decryptAES256
,deriveKey
,decryptEntrophy
,passwordRows
,passwordCols
......@@ -19,33 +23,36 @@ import System.Random
import Data.List
import Data.Word
import Data.Bits
import Data.Char
import qualified Data.ByteString as B
import Crypto.Cipher.AES (AES256)
import Crypto.Cipher.Types (BlockCipher(..), Cipher(..),IV, makeIV)
import Crypto.Error (throwCryptoError)
import Crypto.Random(getSystemDRG, randomBytesGenerate)
import Crypto.KDF.Scrypt (generate, Parameters(..))
saltSize = 64
paramN = 64:: Word64
paramR = 8
paramP = 1
paramKeyLen = 256
--Scrypt KDF
deriveKey :: B.ByteString -> B.ByteString -> B.ByteString
deriveKey password salt = generate params password salt
where params = Parameters {n = paramN, r = paramR, p = paramP, outputLength = paramKeyLen}
-- for generating the salt
randomString :: Int -> IO B.ByteString
randomString size = do
drg <- getSystemDRG
let (bytes, _) = randomBytesGenerate size drg
return bytes
stringtoWord8 :: [Char] -> [Word8]
stringtoWord8 [] = []
stringtoWord8 (x:xs) = [fromIntegral $ ord x] ++ stringtoWord8 xs
--ASCII 128 bites for ñ áéíóú (adding if need more)
chr2 :: Int -> Char
chr2 x
| x==241 = '~'
| x==225 = 'a'
| x==233 = 'e'
| x==237 = 'i'
| x==243 = 'o'
| x==250 = 'u'
| otherwise = chr x
word8toString :: [Word8] -> [Char]
word8toString [] = []
word8toString (x:xs) = [chr2 $ fromIntegral x] ++ word8toString xs
-- Randon generator, Int List Numbers
-- Need Random g for init (g <- getStdGen, g <- newStdGen)
generator :: (RandomGen g ) => Int -> g -> [Int]
generator n g = take n $ randomRs (0,255) g
......@@ -66,6 +73,9 @@ randomBytesString n g = B.pack $ randomBytes n g
-- Need Int > B.lenght [Word8]
-- msgExtend n bytes :: Word8 + Random Word8
---- Expand text
extendW8 :: (RandomGen g ) => Int -> [Word8] -> g -> [Word8]
extendW8 n a g
| n > B.length (B.pack a) = a ++ (B.unpack (B.take (n - B.length (B.pack a)) (randomBytesString n g)))
......@@ -100,9 +110,41 @@ decryptEntrophy n a =
a2 = B.unpack $ B.drop n a
in B.pack (zipWith xor a1 a2)
-- AES ciPher Lv2
encryptAES256 :: B.ByteString -> IV AES256 -> B.ByteString -> B.ByteString
encryptAES256 key cIV plainData = cbcEncrypt ctx cIV plainData
where ctx :: AES256
ctx = throwCryptoError $ cipherInit key
decryptAES256 :: B.ByteString -> IV AES256 -> B.ByteString -> B.ByteString
decryptAES256 key cIV plainData = cbcDecrypt ctx cIV plainData
where ctx :: AES256
ctx = throwCryptoError $ cipherInit key
-- NukeBull Cipher Level 3
saltSize = 64
paramN = 32:: Word64
paramR = 8
paramP = 1
paramKeyLen = 256
--Scrypt KDF
deriveKey :: B.ByteString -> B.ByteString -> B.ByteString
deriveKey password salt = generate params password salt
where params = Parameters {n = paramN, r = paramR, p = paramP, outputLength = paramKeyLen}
-- for generating the salt
randomString :: Int -> IO B.ByteString
randomString size = do
drg <- getSystemDRG
let (bytes, _) = randomBytesGenerate size drg
return bytes
-- NukeBull Cipher
passwordRows :: [Word8] -> [Int]
passwordRows x = map (`div` 16) (map fromIntegral x)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment