SHA 256带印地 [英] SHA 256 With Indy

查看:136
本文介绍了SHA 256带印地的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用SHA-256加密功能,但没有成功.

I am trying to use the SHA-256 encryption function, but with no success.

我需要获取字符串和文件的哈希.我正在使用Delphi 10.1 Berlin和Indy for Hash.

I need to get a hash of a string and a file. I'm using Delphi 10.1 Berlin, and Indy for Hash.

我的代码:

uses System.Classes, IdHashSha, System.SysUtils;

function GetHashF(_filename: string): string;
  var
   sha: TIdHashSHA256;
   fs: TFileStream;
  begin
   if TIdHashSHA256.IsAvailable then
    begin
     sha:= TIdHashSHA256.Create;
     try
      fs:= TFileStream.Create(_filename, fmOpenRead);
      try
       Result:= sha.HashStreamAsHex(fs);
      finally
       sha.Free;
      end;
     finally
      fs.Free;
     end;
    end;

 function GetHashS(_string: string): string;
  var
   sha: TIdHashSHA256;
  begin
   if TIdHashSHA256.IsAvailable then
    begin
     sha:= TIdHashSHA256.Create;
     try
      Result:= sha.HashStringAsHex(_string);
     finally
      sha.Free;
     end;
    end;
  end;

但是,每当执行此操作时,它都会为两个函数返回一个干净的字符串(").
我使用断点检查它是否从 IsAvaible 传递,但不是.发生了什么事?

But whenever I do this, it returns a clean string ("") for both function.
I used the breakpoint to check if it is passing from IsAvaible, and it is not. What is going on?

推荐答案

目前,Indy仅本地实现了一些哈希算法,但不包括SHA-256(我有为此功能创建了票证.

At this time, Indy only implements a few hashing algorithms natively, but that does not include SHA-256 (I have created a ticket for that feature).

对于不是本机实现的算法,Indy允许您挂钩选择的外部哈希实现以提供实际的哈希功能.您正在看到 IsAvailable 返回false,因为您尚未完成该连接.

For algorithms not implemented natively, Indy allows you to hook in an external hashing implementation of your choosing to provide the actual hash functionality. You are seeing IsAvailable return false because you have not done that hookup yet.

只要将适当的包装函数分配给 IdFIPS 单元中的以下回调,您就可以使用所需的任何哈希实现:

You can use any hash implementation you want, as long as you assign appropriate wrapper functions to the following callbacks in the IdFIPS unit:

IsHashingIntfAvail
UpdateHashInst
FinalHashInst

对于SHA-256来说,也是:

And for SHA-256 specifically, also:

GetSHA256HashInst
IsSHA256HashIntfAvail

默认情况下,Indy可以将OpenSSL用作哈希库:

By default, Indy can use OpenSSL as a hashing library:

  • IdSSLOpenSSLHeaders 单元添加到您的 uses 子句

在运行时调用 IdSSLOpenSSLHeaders.Load()

使用您的应用可执行文件部署OpenSSL二进制文件.

deploy the OpenSSL binaries with your app executable.

IdSSLOpenSSLHeaders.pas IdFIPS 回调连接了相关的OpenSSL函数.

IdSSLOpenSSLHeaders.pas hooks up relevant OpenSSL functions for the IdFIPS callbacks.

这篇关于SHA 256带印地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆