PR#1835: add function Digest.from_hex
git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@12082 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02master
parent
ff00570de4
commit
7092fb36d6
2
VERSION
2
VERSION
|
@ -1,4 +1,4 @@
|
|||
3.13.0+dev10 (2012-01-10)
|
||||
3.13.0+dev11 (2012-01-26)
|
||||
|
||||
# The version string is the first line of this file.
|
||||
# It must be in the format described in stdlib/sys.mli
|
||||
|
|
|
@ -50,3 +50,19 @@ let to_hex d =
|
|||
String.blit (Printf.sprintf "%02x" (int_of_char d.[i])) 0 result (2*i) 2;
|
||||
done;
|
||||
result
|
||||
|
||||
let from_hex s =
|
||||
if String.length s <> 32 then raise (Invalid_argument "Digest.from_hex");
|
||||
let digit c =
|
||||
match c with
|
||||
| '0'..'9' -> Char.code c - Char.code '0'
|
||||
| 'A'..'F' -> Char.code c - Char.code 'A' + 10
|
||||
| 'a'..'f' -> Char.code c - Char.code 'a' + 10
|
||||
| _ -> raise (Invalid_argument "Digest.from_hex")
|
||||
in
|
||||
let byte i = digit s.[i] lsl 4 + digit s.[i+1] in
|
||||
let result = String.create 16 in
|
||||
for i = 0 to 15 do
|
||||
result.[i] <- Char.chr (byte (2 * i));
|
||||
done;
|
||||
result
|
||||
|
|
|
@ -61,3 +61,8 @@ val input : in_channel -> t
|
|||
|
||||
val to_hex : t -> string
|
||||
(** Return the printable hexadecimal representation of the given digest. *)
|
||||
|
||||
val from_hex : string -> t
|
||||
(** Convert a hexadecimal representation back into the corresponding digest.
|
||||
Raise [Invalid_argument] if the argument is not exactly 32 hexadecimal
|
||||
characters. *)
|
||||
|
|
Loading…
Reference in New Issue