...
 
Commits (2)
(library
(name yayakaChain)
(libraries core bos))
(libraries core bos hex base64))
......@@ -4,17 +4,18 @@ open Bos
type t = { algorithm : HashAlgorithm.t; hash : string }
let hash hashAlgorithm text =
let hash algorithm text =
let unwrap = function
| Ok result ->
String.chop_prefix_exn ~prefix:"(stdin)= " result
let hex = String.chop_prefix_exn ~prefix:"(stdin)= " result in
Hex.to_string (`Hex hex)
| _ -> raise (Failure "") in
match hashAlgorithm with
match algorithm with
| Sha1 ->
let cmd = Cmd.(v "openssl" % "dgst" % "-sha1" % "-hex") in
let result = OS.Cmd.(run_io cmd (in_string text) |> to_string) in
unwrap result
{ algorithm = algorithm; hash = unwrap result }
| Sha256 ->
let cmd = Cmd.(v "openssl" % "dgst" % "-sha256" % "-hex") in
let result = OS.Cmd.(run_io cmd (in_string text) |> to_string) in
unwrap result
{ algorithm = algorithm; hash = unwrap result }
......@@ -2,7 +2,7 @@ type t =
| Sha1
| Sha256
let from_string = function
let of_string = function
| "Sha-1" -> Some Sha1
| "Sha-2" -> Some Sha256
| _ -> None
......
type t = { value : int }
let from_int value =
let of_int value =
if value > 2147483647 then None else
if value < 0 then None else
Some { value = value }
......
type t
val from_int : int -> t option
val of_int : int -> t option
(** convert int to Power.t *)
val to_int : t -> int
......
......@@ -9,17 +9,17 @@ let pp ppf value =
let hash_algorithm = Test.testable pp ( = )
let test_from_string () =
Test.(check (option hash_algorithm)) "Sha-1" (Some Sha1) (from_string "Sha-1");
Test.(check (option hash_algorithm)) "Sha-2" (Some Sha256) (from_string "Sha-2");
Test.(check (option hash_algorithm)) "Sha256" None (from_string "Sha256")
let test_of_string () =
Test.(check (option hash_algorithm)) "Sha-1" (Some Sha1) (of_string "Sha-1");
Test.(check (option hash_algorithm)) "Sha-2" (Some Sha256) (of_string "Sha-2");
Test.(check (option hash_algorithm)) "Sha256" None (of_string "Sha256")
let test_to_string () =
Test.(check string) "Sha-1" "Sha-1" (to_string Sha1);
Test.(check string) "Sha-2" "Sha-2" (to_string Sha256)
let hash_algorithm_tests = [
"from_string", `Quick, test_from_string;
"of_string", `Quick, test_of_string;
"to_string", `Quick, test_to_string;
]
......
open YayakaChain
module Test = Alcotest
let pp ppf = function
| Hash.{ algorithm = Sha1; hash } -> Fmt.pf ppf "Sha1 %s" (Hex.(of_string hash |> show))
| Hash.{ algorithm = Sha256; hash } -> Fmt.pf ppf "Sha256 %s" (Hex.(of_string hash |> show))
let hash = Test.testable pp ( = )
let h hex = Hex.to_string (`Hex hex)
let test_hash () =
Test.(check string) "a with sha1"
"86f7e437faa5a7fce15d1ddcb9eaeaea377667b8"
(Hash.hash HashAlgorithm.Sha1 "a");
Test.(check string) "abc with sha1"
"a9993e364706816aba3e25717850c26c9cd0d89d"
(Hash.hash HashAlgorithm.Sha1 "abc");
Test.(check string) "a with sha256"
"ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb"
(Hash.hash HashAlgorithm.Sha256 "a");
Test.(check string) "abc with sha256"
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
(Hash.hash HashAlgorithm.Sha256 "abc")
Test.(check hash) "a with sha1" {
algorithm = Sha1;
hash = (h "86f7e437faa5a7fce15d1ddcb9eaeaea377667b8") }
(Hash.hash Sha1 "a");
Test.(check hash) "abc with sha1" {
algorithm = Sha1;
hash = (h "a9993e364706816aba3e25717850c26c9cd0d89d") }
(Hash.hash Sha1 "abc");
Test.(check hash) "a with sha256" {
algorithm = Sha256;
hash = (h "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb") }
(Hash.hash Sha256 "a");
Test.(check hash) "abc with sha256" {
algorithm = Sha256;
hash = (h "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad") }
(Hash.hash Sha256 "abc")
let hash_tests = [
"hash", `Quick, test_hash;
......
......@@ -4,17 +4,17 @@ module Test = Alcotest
let to_int_option = Option.map ~f:Power.to_int
let to_power value =
match (Power.from_int value) with
match (Power.of_int value) with
| Some power -> power
| _ -> raise (Failure "")
let test_from_int () =
Test.(check (option int)) "0" (Some 0) (to_int_option (Power.from_int 0));
Test.(check (option int)) "1,000,000" (Some 1000000) (to_int_option (Power.from_int 1000000));
Test.(check (option int)) "2 ^ 31 - 1" (Some 2147483647) (to_int_option (Power.from_int 2147483647));
Test.(check (option int)) "2 ^ 31" None (to_int_option (Power.from_int 2147483648));
Test.(check (option int)) "-1" None (to_int_option (Power.from_int ~- 1));
Test.(check (option int)) "-1,000,000" None (to_int_option (Power.from_int ~- 1000000))
let test_of_int () =
Test.(check (option int)) "0" (Some 0) (to_int_option (Power.of_int 0));
Test.(check (option int)) "1,000,000" (Some 1000000) (to_int_option (Power.of_int 1000000));
Test.(check (option int)) "2 ^ 31 - 1" (Some 2147483647) (to_int_option (Power.of_int 2147483647));
Test.(check (option int)) "2 ^ 31" None (to_int_option (Power.of_int 2147483648));
Test.(check (option int)) "-1" None (to_int_option (Power.of_int ~- 1));
Test.(check (option int)) "-1,000,000" None (to_int_option (Power.of_int ~- 1000000))
let test_to_int () =
Test.(check int) "0" 0 (Power.to_int (to_power 0));
......@@ -22,7 +22,7 @@ let test_to_int () =
Test.(check int) "2 ^ 31 - 1" 2147483647 (Power.to_int (to_power 2147483647))
let power_tests = [
"from_int", `Quick, test_from_int;
"of_int", `Quick, test_of_int;
"to_int", `Quick, test_to_int;
]
......
......@@ -13,5 +13,7 @@ depends: [
"alcotest" {with-test}
"core"
"bos"
"hex"
"base64"
]
synopsis: "Manipulates yayaka-chains"