zoukankan      html  css  js  c++  java
  • DELPHI HMAC256

    DELPHI HMAC256
     
    unit HMAC;

    interface

    uses
      System.SysUtils,
      EncdDecd,
      IdHMAC,
      IdSSLOpenSSL,
      IdHash;

    type
      THMACUtils = class
      public
        class function HMAC(aKey, aMessage: RawByteString): TBytes;
        class function HMAC_HexStr(aKey, aMessage: RawByteString): RawByteString;
        class function HMAC_Base64(aKey, aMessage: RawByteString): RawByteString;
      end;

    implementation

    class function THMACUtils.HMAC(aKey, aMessage: RawByteString): TBytes;
    var
      _HMAC: T;
    begin
      if not IdSSLOpenSSL.LoadOpenSSLLibrary then Exit;
      _HMAC:= T.Create;
      try
        _HMAC.Key := BytesOf(aKey);
        Result:= _HMAC.HashValue(BytesOf(aMessage));
      finally
        _HMAC.Free;
      end;
    end;

    class function THMACUtils.HMAC_HexStr(aKey, aMessage: RawByteString): RawByteString;
    var
      I: Byte;
    begin
      Result:= '0x';
      for I in HMAC(aKey, aMessage) do
        Result:= Result + IntToHex(I, 2);
    end;

    class function THMACUtils.HMAC_Base64(aKey, aMessage: RawByteString): RawByteString;
    var
      _HMAC: TBytes;
    begin
      _HMAC:= HMAC(aKey, aMessage);
      Result:= EncodeBase64(_HMAC, Length(_HMAC));
    end;

    end.

    下面是调用的例子:

    program HMACSample;

    {$APPTYPE CONSOLE}

    {$R *.res}

    uses
      System.SysUtils,
      HMAC,
      IdHMACSHA1,
      IdHashMessageDigest;

    begin
      try
        Write('HMAC_SHA1("key", "message")'#9#9'= ');
        Writeln(THMACUtils.HMAC_HexStr('key', 'message' ));
        Writeln;

        Write('HMAC_SHA256("key", "message")'#9#9'= ');
        Writeln(THMACUtils.HMAC_HexStr('key', 'message' ));
        Writeln;

        Write('HMAC_SHA1_Base64("key", "message")'#9'= ');
        Writeln(THMACUtils.HMAC_Base64('key', 'message' ));
        Writeln;

        Write('HMAC_SHA256_Base64("key", "message")'#9'= ');
        Writeln(THMACUtils.HMAC_Base64('key', 'message' ));

        Readln;

      except
        on E: Exception do
          Writeln(E.ClassName, ': ', E.Message);
      end;
    end.
  • 相关阅读:
    详解Paint的setPathEffect(PathEffect effect)
    详解Paint的setMaskFilter(MaskFilter maskfilter)
    用TextPaint来绘制文字
    详解Paint的setXfermode(Xfermode xfermode)
    详解Paint的setColorFilter(ColorFilter filter)
    ASP.NET Core 高性能开发最佳实践
    疫苗,为啥还要等这么久?
    物联网行业的基本系统架构
    超级强大的学习方法--费曼技巧
    衡水中学学霸李江珊,高考英语149分,从不刷题的她如何做到的
  • 原文地址:https://www.cnblogs.com/hnxxcxg/p/7735031.html
Copyright © 2011-2022 走看看