...
 
Commits (2)
......@@ -8,6 +8,9 @@
*.cmxs
*.cmxa
# opam
_opam
# ocamlbuild working directory
_build/
......
(library
(name yayakaChain)
(libraries core digestif))
(libraries core bos))
open Core
open HashAlgorithm
open Bos
type t = { algorithm : HashAlgorithm.t; hash : string }
let hash hashAlgorithm text =
let unwrap = function
| Ok result ->
String.chop_prefix_exn ~prefix:"(stdin)= " result
| _ -> raise (Failure "") in
match hashAlgorithm 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
| 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
type t =
| SHA1
| SHA2
| Sha1
| Sha256
let from_string = function
| "SHA-1" -> Some SHA1
| "SHA-2" -> Some SHA2
| "Sha-1" -> Some Sha1
| "Sha-2" -> Some Sha256
| _ -> None
let to_string = function
| SHA1 -> "SHA-1"
| SHA2 -> "SHA-2"
| Sha1 -> "Sha-1"
| Sha256 -> "Sha-2"
......@@ -4,19 +4,19 @@ module Test = Alcotest
let pp ppf value =
match value with
| SHA1 -> Fmt.pf ppf "SHA1"
| SHA2 -> Fmt.pf ppf "SHA2"
| Sha1 -> Fmt.pf ppf "Sha1"
| Sha256 -> Fmt.pf ppf "Sha256"
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 SHA2) (from_string "SHA-2");
Test.(check (option hash_algorithm)) "SHA2" None (from_string "SHA2")
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_to_string () =
Test.(check string) "SHA-1" "SHA-1" (to_string SHA1);
Test.(check string) "SHA-2" "SHA-2" (to_string SHA2)
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;
......
open YayakaChain
module Test = Alcotest
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")
let hash_tests = [
"hash", `Quick, test_hash;
]
let () =
......
......@@ -5,13 +5,13 @@ authors: ["Ryo Hashiguchi <ryo33@yayaka.org>"]
license: "AGPL"
bug-reports: "https://gitlib.com/yayaka/yayaka-chain/issues"
homepage: "https://gitlib.com/yayaka/yayaka-chain"
build-test: [
run-test: [
"dune runtest"
]
depends: [
"dune" {build}
"alcotest" {test}
"alcotest" {with-test}
"core"
"digestif"
"bos"
]
synopsis: "Manipulates yayaka-chains"