|
| 1 | +import Cli |
| 2 | +import Crypto.API |
| 3 | + |
| 4 | +open Cli |
| 5 | +open Crypto |
| 6 | + |
| 7 | + |
| 8 | +def getInFile (p : Parsed) : System.FilePath := |
| 9 | + if p.hasFlag "input" then (p.flag! "input").value else "/dev/stdin" |
| 10 | + |
| 11 | +def getOutFile (p : Parsed) : System.FilePath := |
| 12 | + if p.hasFlag "output" then (p.flag! "output").value else "/dev/stdout" |
| 13 | + |
| 14 | + |
| 15 | +def bytesToHexExec (p : Parsed) : IO UInt32 := |
| 16 | + do |
| 17 | + API.bytesToHex (getInFile p) (getOutFile p) |
| 18 | + pure 0 |
| 19 | + |
| 20 | +def bytesToHex : Cmd := `[Cli| |
| 21 | + bytesToHex VIA bytesToHexExec; |
| 22 | + "Convert bytes to hexadecimal." |
| 23 | + FLAGS: |
| 24 | + i, input : String ; "Input file." |
| 25 | + o, output : String ; "Output file." |
| 26 | +] |
| 27 | + |
| 28 | + |
| 29 | +def bytesToNatExec (p : Parsed) : IO UInt32 := |
| 30 | + do |
| 31 | + API.bytesToNat (getInFile p) (getOutFile p) |
| 32 | + pure 0 |
| 33 | + |
| 34 | +def bytesToNat : Cmd := `[Cli| |
| 35 | + bytesToNat VIA bytesToNatExec; |
| 36 | + "Convert bytes to a natural number." |
| 37 | + FLAGS: |
| 38 | + i, input : String ; "Input file." |
| 39 | + o, output : String ; "Output file." |
| 40 | +] |
| 41 | + |
| 42 | + |
| 43 | +def HashAlg : ParamType := |
| 44 | + ParamType.mk |
| 45 | + "Hash algorithm" |
| 46 | + $ not ∘ BEq.beq none ∘ API.parseHashAlgorithm |
| 47 | + |
| 48 | +def hashExec (p : Parsed) : IO UInt32 := |
| 49 | + match API.parseHashAlgorithm (p.positionalArg! "alg").value with |
| 50 | + | some alg => |
| 51 | + do |
| 52 | + API.hash alg (getInFile p) (getOutFile p) |
| 53 | + pure 0 |
| 54 | + | none => pure 0 |
| 55 | + |
| 56 | +def hash : Cmd := `[Cli| |
| 57 | + hash VIA hashExec; |
| 58 | + "Hash bytes." |
| 59 | + FLAGS: |
| 60 | + i, input : String ; "Input file." |
| 61 | + o, output : String ; "Output file." |
| 62 | + ARGS: |
| 63 | + alg : HashAlg; "SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512" |
| 64 | +] |
| 65 | + |
| 66 | + |
| 67 | +def crypto : Cmd := `[Cli| |
| 68 | + crypto NOOP; |
| 69 | + "Cryptographic operations." |
| 70 | + SUBCOMMANDS: |
| 71 | + bytesToHex; |
| 72 | + bytesToNat; |
| 73 | + hash |
| 74 | +] |
| 75 | + |
| 76 | + |
| 77 | +def main (args : List String) : IO UInt32 := |
| 78 | + crypto.validate args |
0 commit comments