diff options
author | joe <joe@jerkface.net> | 2017-10-16 16:30:24 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2017-10-16 16:30:24 -0400 |
commit | df64a1baba58572fcc2aa82721851ee87f9e55b9 (patch) | |
tree | bd182a20078697360113e36639ec033d47673542 /src/Crypto | |
parent | 2493cd4203d9041295ffbfb144ee2ba23f93c694 (diff) |
Functions {encode|decode}Secret.
Diffstat (limited to 'src/Crypto')
-rw-r--r-- | src/Crypto/Tox.hs | 24 |
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 | ||
48 | import Control.Arrow | 50 | import Control.Arrow |
@@ -53,6 +55,7 @@ import Crypto.ECC.Class | |||
53 | import qualified Crypto.Error as Cryptonite | 55 | import qualified Crypto.Error as Cryptonite |
54 | import qualified Crypto.MAC.Poly1305 as Poly1305 | 56 | import qualified Crypto.MAC.Poly1305 as Poly1305 |
55 | import Crypto.PubKey.Curve25519 | 57 | import Crypto.PubKey.Curve25519 |
58 | import Data.Bits | ||
56 | import qualified Data.ByteArray as BA | 59 | import qualified Data.ByteArray as BA |
57 | ;import Data.ByteArray as BA (ByteArrayAccess, Bytes) | 60 | ;import Data.ByteArray as BA (ByteArrayAccess, Bytes) |
58 | import Data.ByteString as B | 61 | import Data.ByteString as B |
@@ -356,3 +359,24 @@ getPublicKey = throwCryptoError . publicKey <$> S.getBytes 32 | |||
356 | putPublicKey :: PublicKey -> S.Put | 359 | putPublicKey :: PublicKey -> S.Put |
357 | putPublicKey bs = S.putByteString $ BA.convert bs | 360 | putPublicKey bs = S.putByteString $ BA.convert bs |
358 | 361 | ||
362 | encodeSecret :: BA.ByteArrayAccess bin => bin -> Maybe C8.ByteString | ||
363 | encodeSecret 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 | |||
372 | decodeSecret :: C8.ByteString -> Maybe C8.ByteString | ||
373 | decodeSecret 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) | ||