+1 vote

Hello all,

Trying to connect to GameAnalytics REST API from a game I'm building, but HMAC-SHA256 is mandatory and, since I couldn't find a built-in function in GDscript, I'm trying to implement myself. When done, will be a true cross-platform solution for game analytics, no engine recompiling and stuff.

However, tried to no avail and the hash result I get differ from the expected result find in sites like https://www.freeformatter.com/hmac-generator.html

Tried several HMAC-SHA256 generators and they all produce the same output, the bug is in my code.

Data I'm using:

Key: 0123456789012345678901234567890123456789012345678901234567890123
Message: 0123456789012345678901234567890123456789012345678901234567890123ABCD

Using a fixed length key to debug for now.

What I'm doing wrong ??

Code below.

Yes, the code is redundant, ugly, crazy, etc; it's a mix of trying to pinpoint where it went wrong with my recent (1 week) acquired GDscript skills.


func hmac(key, message):
var x = 0
var i = "".toascii()
var o = "".to
ascii()
var m = message.toascii()
var k = key.to
ascii()
var s = File.new()

while x < 64:
    o.append(k[x] ^ 0x5c)
    x += 1

x = 0
while x < 64:
    i.append(k[x] ^ 0x36)
    x += 1

var temp1 = i + m

s.open("user://s.save", File.WRITE)
s.store_buffer(temp1)
s.close()
var z = s.get_sha256("user://s.save")
var temp2  = z.to_ascii()

var temp3 = o + temp2

s.open("user://s.save", File.WRITE)
s.store_buffer(temp3)
s.close()

z = s.get_sha256("user://s.save")

return z
in Engine by (16 points)

SUCCESS!!

Managed to fix all. See here:

https://github.com/Montecri/Godot-GameAnalytics

2 Answers

0 votes

Just a peanut gallery comment that perhaps this would make a cool gdnative addon written in c++

by (30 points)

Hi!

Implemented it myself in GDScript. Check here:

https://github.com/Montecri/Godot-GameAnalytics

0 votes

I found this library which performs the task for 3.1:

https://github.com/johngirvin/godot-ncrypt

by (28 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.