From patchwork Mon Oct 29 10:28:02 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: [Ada] Add support for returning binary message digest Date: Mon, 29 Oct 2012 00:28:02 -0000 From: Arnaud Charlet X-Patchwork-Id: 194946 Message-Id: <20121029102802.GA30545@adacore.com> To: gcc-patches@gcc.gnu.org Cc: Pascal Obry If a binary digest is needed it is a waste of time to recompute it from the string encoded digest. Tested on x86_64-pc-linux-gnu, committed on trunk 2012-10-29 Pascal Obry * g-sechas.adb, g-sechas.ads: (Binary_Message_Digest): New subtype. (Digest): New versions returning a Binary_Message_Digest. (Wide_Digest): Likewise. Index: g-sechas.adb =================================================================== --- g-sechas.adb (revision 192920) +++ g-sechas.adb (working copy) @@ -184,6 +184,30 @@ return Digest (C); end Digest; + function Digest (C : Context) return Binary_Message_Digest is + Hash_Bits : Stream_Element_Array + (1 .. Stream_Element_Offset (Hash_Length)); + begin + Final (C, Hash_Bits); + return Hash_Bits; + end Digest; + + function Digest (S : String) return Binary_Message_Digest is + C : Context; + begin + Update (C, S); + return Digest (C); + end Digest; + + function Digest + (A : Stream_Element_Array) return Binary_Message_Digest + is + C : Context; + begin + Update (C, A); + return Digest (C); + end Digest; + ----------- -- Final -- ----------- @@ -325,6 +349,13 @@ return Digest (C); end Wide_Digest; + function Wide_Digest (W : Wide_String) return Binary_Message_Digest is + C : Context; + begin + Wide_Update (C, W); + return Digest (C); + end Wide_Digest; + end H; ------------------------- Index: g-sechas.ads =================================================================== --- g-sechas.ads (revision 192920) +++ g-sechas.ads (working copy) @@ -156,6 +156,22 @@ Word_Length : constant Natural := Hash_State.Word'Size / 8; Hash_Length : constant Natural := Hash_Words * Word_Length; + subtype Binary_Message_Digest is + Stream_Element_Array (1 .. Stream_Element_Offset (Hash_Length)); + -- The fixed-length byte array returned by Digest, providing + -- the hash in binary representation. + + function Digest (C : Context) return Binary_Message_Digest; + -- Return hash for the data accumulated with C + + function Digest (S : String) return Binary_Message_Digest; + function Wide_Digest (W : Wide_String) return Binary_Message_Digest; + function Digest + (A : Stream_Element_Array) return Binary_Message_Digest; + -- These functions are equivalent to the corresponding Update (or + -- Wide_Update) on a default initialized Context, followed by Digest + -- on the resulting Context. + subtype Message_Digest is String (1 .. 2 * Hash_Length); -- The fixed-length string returned by Digest, providing the hash in -- hexadecimal representation.