Check that the argument to Digest.to_hex has the correct length (#763)

* Check that the argument to Digest.to_hex has the correct length and improve documentation for Digest.to_hex
master
Fabrice Le Fessant 2016-08-24 15:14:13 +02:00 committed by GitHub
parent c0aeb5600f
commit 657b34244a
2 changed files with 5 additions and 2 deletions

View File

@ -50,6 +50,7 @@ let char_hex n =
Char.unsafe_chr (n + if n < 10 then Char.code '0' else (Char.code 'a' - 10))
let to_hex d =
if String.length d <> 16 then invalid_arg "Digest.to_hex";
let result = Bytes.create 32 in
for i = 0 to 15 do
let x = Char.code d.[i] in
@ -59,7 +60,7 @@ let to_hex d =
Bytes.unsafe_to_string result
let from_hex s =
if String.length s <> 32 then raise (Invalid_argument "Digest.from_hex");
if String.length s <> 32 then invalid_arg "Digest.from_hex";
let digit c =
match c with
| '0'..'9' -> Char.code c - Char.code '0'

View File

@ -73,7 +73,9 @@ val input : in_channel -> t
(** Read a digest from the given input channel. *)
val to_hex : t -> string
(** Return the printable hexadecimal representation of the given digest. *)
(** Return the printable hexadecimal representation of the given digest.
Raise [Invalid_argument] if the argument is not exactly 16 bytes.
*)
val from_hex : string -> t
(** Convert a hexadecimal representation back into the corresponding digest.