Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ckw committed Aug 12, 2014
0 parents commit 52cf17f
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cabal.sandbox.config
.cabal-sandbox
*.un~
*.hi
*.o
dist
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2014 ckw

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2 changes: 2 additions & 0 deletions Setup.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import Distribution.Simple
main = defaultMain
1 change: 1 addition & 0 deletions cabal.config
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
constraints: bytestring == 0.10.4.0
27 changes: 27 additions & 0 deletions cryptopals.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-- Initial cryptopals.cabal generated by cabal init. For further
-- documentation, see http://haskell.org/cabal/users-guide/

name: cryptopals
version: 0.1.0.0
-- synopsis:
-- description:
license: MIT
license-file: LICENSE
author: Connor Williams
maintainer: [email protected]
-- copyright:
-- category:
build-type: Simple
-- extra-source-files:
cabal-version: >=1.10

executable set1ch1
main-is: challenge1.hs
ghc-options: -Wall
-- other-modules:
-- other-extensions:
build-depends: base >=4.5 && <4.6
, bytestring == 0.10.4.0
, base64-bytestring == 1.0.0.1
hs-source-dirs: set1, src
default-language: Haskell2010
5 changes: 5 additions & 0 deletions set1/challenge1.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import qualified Data.ByteString.Lazy as BL
import Crypto.Common (toB64, toHex)

main :: IO ()
main = BL.getContents >>= BL.putStr . toB64 . toHex
49 changes: 49 additions & 0 deletions src/Crypto/Common.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
module Crypto.Common
( toHex
, toB64
)
where

import Data.Bits ((.|.), shiftL)
import qualified Data.ByteString.Base64.Lazy as B64 (encode)
import qualified Data.ByteString.Lazy as BL
import Data.Word
import Data.Char (chr)

toB64 :: BL.ByteString -> BL.ByteString
toB64 = B64.encode

toHex :: BL.ByteString -> BL.ByteString
toHex bs = BL.pack $ toHex' (BL.unpack bs)
where toHex' :: [Word8] -> [Word8]
toHex' [] = []
toHex' [x] = [shiftL x 4]
toHex' (x:y:xs) = ((shiftL (conv x) 4) .|. (conv y)) : toHex' xs
conv :: Word8 -> Word8
conv x = case x of
0x30 -> 0x0
0x31 -> 0x1
0x32 -> 0x2
0x33 -> 0x3
0x34 -> 0x4
0x35 -> 0x5
0x36 -> 0x6
0x37 -> 0x7
0x38 -> 0x8
0x39 -> 0x9

0x41 -> 0xa
0x42 -> 0xb
0x43 -> 0xc
0x44 -> 0xd
0x45 -> 0xe
0x46 -> 0xf

0x61 -> 0xa
0x62 -> 0xb
0x63 -> 0xc
0x64 -> 0xd
0x65 -> 0xe
0x66 -> 0xf

c -> error $ "invalid hex character: " ++ (show . chr . fromIntegral $ c)

0 comments on commit 52cf17f

Please sign in to comment.