Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
H
haskell-gargantext-prelude
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
1
Issues
1
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
gargantext
haskell-gargantext-prelude
Commits
03c3c381
Commit
03c3c381
authored
Oct 10, 2022
by
Alexandre Delanoë
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
missing file
parent
e305f9c5
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
76 additions
and
0 deletions
+76
-0
Symmetric.hs
src/Gargantext/Prelude/Crypto/Symmetric.hs
+76
-0
No files found.
src/Gargantext/Prelude/Crypto/Symmetric.hs
0 → 100644
View file @
03c3c381
{-|
Module : Gargantext.Prelude.Crypto.Symmetric
Description :
Copyright : (c) CNRS, 2017-Present
License : AGPL + CECILL v3
Maintainer : team@gargantext.org
Stability : experimental
Portability : POSIX
-}
{-# LANGUAGE PackageImports #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE GADTs #-}
module
Gargantext.Prelude.Crypto.Symmetric
where
import
"cryptonite"
Crypto.Cipher.AES
(
AES256
)
import
Crypto.Cipher.Types
(
BlockCipher
(
..
),
Cipher
(
..
),
nullIV
,
KeySizeSpecifier
(
..
),
IV
,
makeIV
)
import
Crypto.Error
(
CryptoFailable
(
..
),
CryptoError
(
..
))
import
Data.ByteArray
(
ByteArray
)
import
Data.ByteString
(
ByteString
)
import
Data.Either
import
Data.Maybe
import
Protolude
-- (IO, Int, show, (++), ($), putStrLn, panic, (<*>), (<$>), (=<<), undefined, return)
import
qualified
"cryptonite"
Crypto.Random.Types
as
CRT
-- | Not required, but most general implementation
data
Key
c
a
where
Key
::
(
BlockCipher
c
,
ByteArray
a
)
=>
a
->
Key
c
a
-- | Generates a string of bytes (key) of a specific length for a given block cipher
genSecretKey
::
forall
m
c
a
.
(
CRT
.
MonadRandom
m
,
BlockCipher
c
,
ByteArray
a
)
=>
c
->
Int
->
m
(
Key
c
a
)
genSecretKey
_
=
fmap
Key
.
CRT
.
getRandomBytes
-- | Generate a random initialization vector for a given block cipher
genRandomIV
::
forall
m
c
.
(
CRT
.
MonadRandom
m
,
BlockCipher
c
)
=>
c
->
m
(
Maybe
(
IV
c
))
genRandomIV
_
=
do
bytes
::
ByteString
<-
CRT
.
getRandomBytes
$
blockSize
(
undefined
::
c
)
return
$
makeIV
bytes
-- | Initialize a block cipher
initCipher
::
(
BlockCipher
c
,
ByteArray
a
)
=>
Key
c
a
->
Either
CryptoError
c
initCipher
(
Key
k
)
=
case
cipherInit
k
of
CryptoFailed
e
->
Left
e
CryptoPassed
a
->
Right
a
encrypt
::
(
BlockCipher
c
,
ByteArray
a
)
=>
Key
c
a
->
IV
c
->
a
->
Either
CryptoError
a
encrypt
secretKey
initIV
msg
=
case
initCipher
secretKey
of
Left
e
->
Left
e
Right
c
->
Right
$
ctrCombine
c
initIV
msg
decrypt
::
(
BlockCipher
c
,
ByteArray
a
)
=>
Key
c
a
->
IV
c
->
a
->
Either
CryptoError
a
decrypt
=
encrypt
exampleAES256
::
ByteString
->
IO
()
exampleAES256
msg
=
do
-- secret key needs 256 bits (32 * 8)
secretKey
<-
genSecretKey
(
undefined
::
AES256
)
32
mInitIV
<-
genRandomIV
(
undefined
::
AES256
)
case
mInitIV
of
Nothing
->
panic
"Failed to generate and initialization vector."
Just
initIV
->
do
let
encryptedMsg
=
encrypt
secretKey
initIV
msg
decryptedMsg
=
decrypt
secretKey
initIV
=<<
encryptedMsg
case
(,)
<$>
encryptedMsg
<*>
decryptedMsg
of
Left
err
->
panic
$
show
err
Right
(
eMsg
,
dMsg
)
->
do
putStrLn
$
"Original Message: "
++
show
msg
putStrLn
$
"Message after encryption: "
++
show
eMsg
putStrLn
$
"Message after decryption: "
++
show
dMsg
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment