zoukankan      html  css  js  c++  java
  • Delphi XE7 用indy开发微信公众平台(3)- 验证消息真实性

    验证消息真实性

    原文链接:http://www.cnblogs.com/devinlee/p/4282546.html

    扫下方二维码关注,测试效果

    uses IdHashSHA, IdGlobal;
    
    function SHA1(Input: String): String;
    begin
      with TIdHashSHA1.Create do
        try
          Result := LowerCase(HashBytesAsHex(TidBytes(Bytesof(Input))));
        finally
          Free;
        end;
    end;
    
    function CheckSignature(ARequestInfo: TIdHTTPRequestInfo): boolean;
    var
      signature, timestamp, nonce, echostr: String;
      tmpstr: TStringList;
      temp: String;
    begin
      tmpstr := TStringList.Create;
      try
        signature := ARequestInfo.Params.Values['signature'];
        timestamp := ARequestInfo.Params.Values['timestamp'];
        nonce := ARequestInfo.Params.Values['nonce'];
    
        echostr := ARequestInfo.Params.Values['echostr'];
        tmpstr.Add(Token);
        tmpstr.Add(timestamp);
        tmpstr.Add(nonce);
        tmpstr.Sort;
        temp := StringReplace(tmpstr.text, #13#10, '', [rfReplaceAll]);
        Result := SHA1(temp) = signature;
      finally
        tmpstr.Free;
      end;
    end;
    
    procedure TForm1.IdHTTPServerCommandGet(AContext: TIdContext;
      ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
    begin
      if CheckSignature(ARequestInfo) then
        if ARequestInfo.Params.Values['echostr'] <> '' then
        begin
          AResponseInfo.ContentType := 'text/html; charset=UTF-8';
          AResponseInfo.ContentText := ARequestInfo.Params.Values['echostr'];
        end;
    end;
  • 相关阅读:
    8.22
    webstrom安装流程
    8.21
    8.20
    8.20学习笔记
    使用WebClient异步获取http资源
    导航栏,可直接使用
    asp.net mvc5实现单点登录
    使用C#调用Word的接口生成doc文件与html文件
    下载网页并保存
  • 原文地址:https://www.cnblogs.com/devinlee/p/4282546.html
Copyright © 2011-2022 走看看