码迷,mamicode.com
首页 > 微信 > 详细

Delphi XE7 用indy开发微信公众平台(3)- 验证消息真实性

时间:2015-02-09 22:49:29      阅读:3028      评论:0      收藏:0      [点我收藏+]

标签:

验证消息真实性

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;

 

原文地址:http://www.cnblogs.com/devinlee/p/4282546.html

Delphi XE7 用indy开发微信公众平台(3)- 验证消息真实性

标签:

原文地址:http://www.cnblogs.com/devinlee/p/4282546.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!