Quickblox Basic4Android签名生成

时间:2013-11-29 22:47:07

标签: quickblox basic4android

尝试创建QuickBlox会话时,我收到无效的签名错误:

Private Sub GenerateSignature() As String
Dim timestamp As Long : timestamp = DateTime.Now / 1000
Dim nonce As Int : nonce = Rnd(1, 1000)

Dim signatureParams As String
signatureParams = "application_id=[appid]&auth_key=[authkey]&nonce=[nonce]&timestamp=[time]".Replace( _
    "[appid]", ApplicationID).Replace("[authkey]", AuthorizationKey).Replace("[nonce]", nonce).Replace("[time]", timestamp)

Dim sig As String : sig = EncryptHMACSHA(signatureParams, AuthorizationSecret)

Log(signatureParams)

Log("Generated Signature for Token: " & sig.ToLowerCase)

Return sig.ToLowerCase
End Sub

Public Sub GetSessionToken()
Dim job As HttpJob
job.Initialize("GetToken", Me)

Dim timestamp As Long : timestamp = DateTime.Now / 1000
Dim nonce As Int : nonce = Rnd(1, 1000)

Dim tokenParams As String
tokenParams = "application_id=[appid]&auth_key=[authkey]&nonce=[nonce]&timestamp=[time]&signature=[sig]".Replace("[appid]", ApplicationID) _
    .Replace("[authkey]", AuthorizationKey).Replace("[nonce]", nonce).Replace("[time]", timestamp).Replace("[sig]", GenerateSignature)

Log(tokenParams)

job.PostString("https://api.quickblox.com/session.xml", tokenParams)
job.GetRequest.SetHeader("QuickBlox-REST-API-Version", "0.1.0")
End Sub

Private Sub EncryptHMACSHA(Target As String, Key As String) As String
Dim m As Mac
Dim k As KeyGenerator
k.Initialize("HMACSHA1")
k.KeyFromBytes(Key.GetBytes("UTF8"))
m.Initialise("HMACSHA1", k.Key)
m.Update(Target.GetBytes("UTF8"))
Dim b() As Byte
b = m.Sign
Dim bc As ByteConverter

Return bc.HexFromBytes(b)
End Sub

我已经检查了加密功能和时间戳与其他在线样本,它似乎正在运行。我有url参数的顺序正确。我仍然无法弄清楚为什么我会得到“意外的签名”。

1 个答案:

答案 0 :(得分:0)

我看到你使用不同的时间戳&用于签名生成的随机数会话创建请求

你应该使用相同的

Dim timestamp As Long : timestamp = DateTime.Now / 1000
Dim nonce As Int : nonce = Rnd(1, 1000)