您的位置:首页 > 编程语言 > Delphi

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

2015-02-09 21:47 351 查看
验证消息真实性

原文链接:/article/5501999.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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐