summaryrefslogtreecommitdiff
path: root/src/Crypto
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2017-10-16 16:30:24 -0400
committerjoe <joe@jerkface.net>2017-10-16 16:30:24 -0400
commitdf64a1baba58572fcc2aa82721851ee87f9e55b9 (patch)
treebd182a20078697360113e36639ec033d47673542 /src/Crypto
parent2493cd4203d9041295ffbfb144ee2ba23f93c694 (diff)
Functions {encode|decode}Secret.
Diffstat (limited to 'src/Crypto')
-rw-r--r--src/Crypto/Tox.hs24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/Crypto/Tox.hs b/src/Crypto/Tox.hs
index c8b3665f..b84e5df6 100644
--- a/src/Crypto/Tox.hs
+++ b/src/Crypto/Tox.hs
@@ -43,6 +43,8 @@ module Crypto.Tox
43 , zeros24 43 , zeros24
44 , decryptSymmetric 44 , decryptSymmetric
45 , encryptSymmetric 45 , encryptSymmetric
46 , encodeSecret
47 , decodeSecret
46 ) where 48 ) where
47 49
48import Control.Arrow 50import Control.Arrow
@@ -53,6 +55,7 @@ import Crypto.ECC.Class
53import qualified Crypto.Error as Cryptonite 55import qualified Crypto.Error as Cryptonite
54import qualified Crypto.MAC.Poly1305 as Poly1305 56import qualified Crypto.MAC.Poly1305 as Poly1305
55import Crypto.PubKey.Curve25519 57import Crypto.PubKey.Curve25519
58import Data.Bits
56import qualified Data.ByteArray as BA 59import qualified Data.ByteArray as BA
57 ;import Data.ByteArray as BA (ByteArrayAccess, Bytes) 60 ;import Data.ByteArray as BA (ByteArrayAccess, Bytes)
58import Data.ByteString as B 61import Data.ByteString as B
@@ -356,3 +359,24 @@ getPublicKey = throwCryptoError . publicKey <$> S.getBytes 32
356putPublicKey :: PublicKey -> S.Put 359putPublicKey :: PublicKey -> S.Put
357putPublicKey bs = S.putByteString $ BA.convert bs 360putPublicKey bs = S.putByteString $ BA.convert bs
358 361
362encodeSecret :: BA.ByteArrayAccess bin => bin -> Maybe C8.ByteString
363encodeSecret k = do
364 (a,bs) <- BA.uncons (BA.convert k)
365 (cs,c) <- unsnoc bs
366 let a' = shiftR a 1 .|. (shiftR c 4 .&. 0x03)
367 c' = shiftL c 4
368 xs = Base64.encode $ cs `BA.snoc` a' `BA.snoc` c'
369 (ys,ds) = BA.splitAt 40 xs
370 return $ BA.index ds 0 `BA.cons` ys `BA.snoc` BA.index ds 1
371
372decodeSecret :: C8.ByteString -> Maybe C8.ByteString
373decodeSecret k64 = do
374 (ds0,ysds1) <- BA.uncons k64
375 (ys,ds1) <- unsnoc ysds1
376 let k64' = B.append ys (BA.cons ds0 (BA.cons ds1 "A="))
377 k <- either (const Nothing) Just $ Base64.decode k64'
378 (csa,c') <- unsnoc k
379 (cs,a') <- unsnoc csa
380 let a = shiftL (a' .&. 0x7c) 1
381 c = shiftR c' 4 .|. (shiftL a' 4 .&. 0x30) .|. 0x40
382 return $ a `BA.cons` (cs `BA.snoc` c)