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

验证消息真实性

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

时间: 2024-10-22 11:35:54

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

Delphi XE7 用indy开发微信公众平台所有功能,可刷阅读,可刷赞,可加推广(除微支付)

http://www.cnblogs.com/devinlee/p/4565933.html Delphi XE7 用indy开发微信公众平台所有功能,可刷阅读,可刷赞,可加推广(除微支付) 关注作者的微信公众平台,测试效果 包含微信公众平台的所有功能(除微支付) 示例代码:(如需要全部代码,请加作者微信:Leedege) 复制代码 function UpNews(Num: Integer; AccessToken: String): String; var J: TJSONObject; N:

Delphi XE7 用indy开发微信公众平台(2)- 获取微信服务器IP地址

获取微信服务器IP地址 const ServerIpUrl = 'https://api.weixin.qq.com/cgi-bin/getcallbackip?&access_token=%s'; procedure GetServerList(out List: TStringList; AccessToken: String); var Url: string; J: TJSONObject; temp: String; begin Url := Format(ServerIpUrl, [

Delphi XE7 用indy开发微信公众平台(4)- 接收事件推送

接收事件推送 type TEventType = (subscribe, unsubscribe, SCAN, LOCATION, CLICK, VIEW); function ResponseEvent(M: TMessage; X: IXMLDocument): RawByteString; var event: TEventType; temp, menu: String; begin temp := X.DocumentElement.ChildNodes.Nodes['Event'].

Delphi XE7 用indy开发微信公众平台(4)- 接收普通消息

接收普通消息 type TMsgType = (event, text, image, voice, video, location, link); TMessage = Record ToUserName: String; FromUserName: String; CreateTime: Integer; MsgType: String; end; uses System.SysUtils, System.JSON, TypInfo, Xml.XMLIntf, Xml.XMLDoc, Act

Delphi XE7 用indy开发微信公众平台(6)- 被动回复用户消息

被动回复用户消息 1.回复文本消息 function ReplyText(Msg: TMessage; MsgText: String): RawByteString; var X: IXMLDocument; begin X := NewXMLDocument; try X.Xml.text := TextMsg; X.Active := true; with X.DocumentElement.ChildNodes do begin Nodes['ToUserName'].NodeValue

Delphi XE7 用indy开发微信公众平台(8)- 自定义菜单

自定义菜单创建接口 1.创建菜单 const CreateMenuUrl = 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token=%s'; function CreateMenus(AccessToken: String): TJSONObject; var Url: string; J: TJSONObject; m1: TJSONObject; m2, m21, m22: TJSONObject; m3, m31, m32,

Delphi XE7 用indy开发微信公众平台(9)- 高级群发接口

高级群发接口 1.上传多媒体文件(这里以上传图片为例) uses IdMultipartFormData; const UpMediaUrl = 'http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=%s&type=%s'; function UpMedia(AccessToken, MediaType, MediaFile: String): String; var J: TJSONObject; Url: Strin

Delphi XE7 用indy开发微信公众平台(7)- 用户管理

用户管理 1.获取用户列表 const FansListUrl = 'https://api.weixin.qq.com/cgi-bin/user/get?access_token=%s&next_openid=%s'; function GetOpenIDList(AccessToken, NextOpenID: String): TStringList; var Url: string; J: TJSONObject; O: TJSONArray; temp: String; begin R

Delphi XE7 用indy开发微信公众平台(1)- 获取access_token、expires_in

获取access_token.expires_in function GetMethod(HTTP: TIdHTTP; Url: String; Max: Integer): String; var RespData: TStringStream; begin RespData := TStringStream.Create('', TEncoding.UTF8); try try HTTP.Get(Url, RespData); HTTP.Request.Referer := Url; Res