Commit 895d8bab authored by Stephane Glondu's avatar Stephane Glondu
Browse files

Move SHA256 wrappers to Util

parent 92bf479f
open Util
open Serializable_t
let hash x = Cryptokit.(x |>
hash_string (Hash.sha256 ()) |>
transform_string (Hexa.encode ())
);;
(* Setup group *)
let g = Z.of_string "14887492224963187634282421537186040801304008017743492304481737382571933937568724473847106029915040150784031882206090286938661464458896494215273989547889201144857352611058572236578734319505128042602372864570426550855201448111746579871811249114781674309062693442442368697449970648232621880001709535143047913661432883287150003429802392229361583608686643243349727791976247247948618930423866180410558458272606627111270040091203073580238905303994472202930783207472394578498507764703191288249547659899997131166130259700604433891232298182348403175947450284433411265966789131024573629546048637848902243503970966798589660808533";;
......@@ -28,7 +23,7 @@ assert (KG.check public_key);;
(* Save to file *)
let id = String.sub
(hash (Z.to_string public_key.trustee_public_key))
(sha256_hex (Z.to_string public_key.trustee_public_key))
0 8 |> String.uppercase
;;
......
......@@ -116,3 +116,13 @@ let rec list_join sep = function
| [] -> []
| [x] -> [x]
| x :: xs -> x :: sep :: list_join sep xs
let sha256_hex x = Cryptokit.(x |>
hash_string (Hash.sha256 ()) |>
transform_string (Hexa.encode ())
)
let sha256_b64 x = Cryptokit.(x |>
hash_string (Hash.sha256 ()) |>
transform_string (Base64.encode_compact ())
)
......@@ -28,3 +28,6 @@ module String : sig
end
val list_join : 'a -> 'a list -> 'a list
val sha256_hex : string -> string
val sha256_b64 : string -> string
......@@ -46,11 +46,6 @@ let load_from_file read fname =
close_in i;
result
let hashB x = Cryptokit.(x |>
hash_string (Hash.sha256 ()) |>
transform_string (Base64.encode_compact ())
)
let load_elections_and_votes dirname =
Lwt_unix.files_of_directory dirname |>
Lwt_stream.filter_map_s (fun x ->
......@@ -84,7 +79,7 @@ let load_elections_and_votes dirname =
load_from_file (Serializable_j.read_result Serializable_builtin_j.read_number)
) with Sys_error _ -> None
in
let fingerprint = hashB raw in
let fingerprint = sha256_b64 raw in
let ballots =
let file = data "ballots.json" in
if Sys.file_exists file then (
......
......@@ -27,7 +27,5 @@ type election_data = {
can_admin : acl;
}
val hashB : string -> string
val load_elections_and_votes :
string -> (election_data * (string * Z.t ballot) Lwt_stream.t) Lwt_stream.t
......@@ -48,7 +48,7 @@ module MakeBallotBox (E : LWT_ELECTION) = struct
) with e -> Lwt.fail (Serialization e)
in
if E.check_ballot ballot then (
Ocsipersist.add ballot_table (Common.hashB rawballot) rawballot >>
Ocsipersist.add ballot_table (sha256_b64 rawballot) rawballot >>
Ocsipersist.add record_table user date
) else (
Lwt.fail ProofCheck
......
......@@ -41,7 +41,7 @@ let () =
let uuid_underscored = String.map (function '-' -> '_' | c -> c) uuid in
let table = Ocsipersist.open_table ("ballots_" ^ uuid_underscored) in
Lwt_stream.iter_s (fun (r, v) ->
Ocsipersist.add table (Common.hashB r) v
Ocsipersist.add table (sha256_b64 r) v
) ballots >>
Ocsipersist.add imported_table uuid true
) else (
......@@ -266,7 +266,7 @@ let () = Eliom_registration.Html5.register
if
Uuidm.equal uuid ballot.election_uuid &&
E.check_ballot ballot
then `Valid (Common.hashB raw_ballot)
then `Valid (sha256_b64 raw_ballot)
else `Invalid
with e -> `Malformed e
in
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment