TradingView
OgahTerkenal
Dec 23, 2021 9:59 PM

OteHmacSha256 

Bitcoin / TetherUSBinance

Description

Library "OteHmacSha256"
Library to use HMAC SHA-256 by OgahTerkenal

hmac_sha256(string) HMAC SHA-256
  Parameters:
    string: msg String to be hashed
  Returns: [string, array] Return a hashed string in hex format and an array of 8 32 bits integer

Library to use HMAC SHA-256 for authenticating alert message going out from TradingView.
It has limitation on allowed characters (because PineScript cannot access the underlying bits of each ASCII) from ASCII 32 to 126 only.
Usage Example section at the end of the source code pretty much tell everything about this library.
General example as how to import to your PineScript code is not included (please refer to the PineScript manual).

Release Notes

Library "OteHmacSha256"
Library to use HMAC SHA-256 by OgahTerkenal

hmac_sha256(string)
  HMAC SHA-256
  Parameters:
    string: msg String to be hashed
  Returns: [string, array] Return a hashed string in hex format and an array of 8 32 bits integer
Comments
stormRidr
Exactly the code I was looking for to "protect" a webhook call. But it fails for me. Any ideas?

USAGE:
import OgahTerkenal/OteHmacSha256/1
hash = hmac_sha256(txt)

ERROR:
Traceback (most recent call last):
Unable to compile the 'OgahTerkenal/OteHmacSha256/1' library: line 353: Undeclared identifier 'e'
line 356: Undeclared identifier 'a'
line 353: Variable 'S1_0' is not found in scope 'hmac_sha256_#2_#0_#3_#0', cannot register side effect.

COMMENTs:
* I looked at the code, but can't see an immediately obvious problem.
* Removing the hmac_sha256() call still causes the error. The error really comes already with the import statement.
stormRidr
Made a copy of the code and finally got it to compile. It look like there was a compiler change at some stage.

FROM: binaryRotateRight(binaryString, rotateLen) =>
TO: binaryRotateRight(string binaryString, int _rotateLen) =>

Code works nicely. Thanks for the effort with the library!
OgahTerkenal
@stormRidr, Sorry for the late response.
Thank you for letting me know about it, I already updated it.
vlmbrjck
Please add a sample indicator with a simple alert to demonstrate your library. I'm jumping from one error to another. A bit more documentation would be fantastic :-)
vlmbrjck
After import, I'm getting this error: Could not find function or function reference 'hmac_sha256' Any suggestions?
dab1rd
This is pretty good and I will need it... probably.. some day..
ea05c5df-f05e-45ad-b8ba-dff1f8
How can i add Secret Key parameter to this library? I'm doing accesses outside of tradingview and got a request like this: Signature = HmacSHA256(secretkey, String)
OgahTerkenal
@ea05c5df-f05e-45ad-b8ba-dff1f8, currently this function doesn't provide a way to separate secret key input. You need to concatenate the secret string into input, and on the other end you could leave the secret key empty. For example in openssl command you can just type:

echo -n "<string>" | openssl dgst -sha256 -hmac

and then you can compare both result.
More