从 GNAT.SHA1 获取 SHA1 块

发布于 2024-12-26 17:53:31 字数 320 浏览 2 评论 0原文

我正在使用 GNAT.SHA1Ada 中创建字符串的 SHA1 哈希值。我的代码库很小,因此我希望避免将编译器未提供的任何库导入到项目中,因此我使用 GNAT.SHA1。据我所知,用于检索哈希值的唯一“公共”方法是通过 Digest 函数以字符串形式。我想获取 160 位块,它是 Context 类型的 H 成员。不幸的是,Context 记录是私有的。我有什么办法可以得到它吗?或者他们是 GNAT 或 Ada 标准库提供的替代方法?

I am using GNAT.SHA1 to create a SHA1 hash of a string in Ada. My code base is small, so I'd like to avoid importing any libraries to the project that aren't provided by my compiler, hence I'm using GNAT.SHA1. As far as I'm aware, the only "public" methods for retrieving the hash is in String form via the Digest function. I would like to instead get the 160-bit block that is the H member of the Context type. Unfortunately, the Context record is private. Is there any way I can get at it? Or is their an alternative method provided by GNAT or the Ada standard library?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ㄟ。诗瑗 2025-01-02 17:53:31

我不知道有什么直接方法可以获取 160 位块,但当然您可以从十六进制字符串计算它。

这是我刚刚拼凑的一个例子。 (当然,通过将 "16#""#" 添加到十六进制子字符串来提取 32 位整数可能不是最优雅的解决方案。

with GNAT.SHA1;
with Interfaces;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
procedure SHA1_Demo is
    C: GNAT.SHA1.Context;
    package U32_IO is new Ada.Text_IO.Modular_IO(Interfaces.Unsigned_32);
    use U32_IO;
begin
    GNAT.SHA1.Update(C, "hello");
    declare
        SHA1: constant String := GNAT.SHA1.Digest(C);
        H: array(0..4) of Interfaces.Unsigned_32;
    begin
        Put_Line("SHA1(""hello"") = " & GNAT.SHA1.Digest(C));
        for I in Integer range 0 .. 4 loop
            H(I) := Interfaces.Unsigned_32'Value
                        ("16#" & SHA1(I*8+1 .. I*8+8) & "#");
            Put("H(");
            Put(I, Width => 0);
            Put(") = ");
            Put(H(I), Base => 16);
            New_Line;
        end loop;
    end;
end SHA1_Demo;

输出为:

SHA1("hello") = aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d
H(0) = 16#AAF4C61D#
H(1) = 16#DCC5E8A2#
H(2) = 16#DABEDE0F#
H(3) = 16#3B482CD9#
H(4) = 16#AEA9434D#

I don't know of any direct way to get the 160-bit block, but of course you can compute it from the hexadecimal string.

Here's an example I just threw together. (Granted, extracting 32-bit integers by adding "16#" and "#" to the hex substring may not be the most elegant solution.

with GNAT.SHA1;
with Interfaces;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
procedure SHA1_Demo is
    C: GNAT.SHA1.Context;
    package U32_IO is new Ada.Text_IO.Modular_IO(Interfaces.Unsigned_32);
    use U32_IO;
begin
    GNAT.SHA1.Update(C, "hello");
    declare
        SHA1: constant String := GNAT.SHA1.Digest(C);
        H: array(0..4) of Interfaces.Unsigned_32;
    begin
        Put_Line("SHA1(""hello"") = " & GNAT.SHA1.Digest(C));
        for I in Integer range 0 .. 4 loop
            H(I) := Interfaces.Unsigned_32'Value
                        ("16#" & SHA1(I*8+1 .. I*8+8) & "#");
            Put("H(");
            Put(I, Width => 0);
            Put(") = ");
            Put(H(I), Base => 16);
            New_Line;
        end loop;
    end;
end SHA1_Demo;

The output is:

SHA1("hello") = aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d
H(0) = 16#AAF4C61D#
H(1) = 16#DCC5E8A2#
H(2) = 16#DABEDE0F#
H(3) = 16#3B482CD9#
H(4) = 16#AEA9434D#
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文