Crypto for secured requests
-
Hi, I'm trying to automate some smart devices I own (through the Tuya Cloud interface) but I need to encrypt the POST request before I can get a token to work with the API.
Anyone knows if it's even possible to use crypt properly?
Ideally, this should be based on TypeScript which I dont think the hub supports (?)
For vanilla Javascript I need to create an SHA256 based string and it breaks on the following -
crypto.subtle.digest('SHA-256', msgBuffer);msgBuffer is just an encoded string which seems to be working -
var msgBuffer = new TextEncoder().encode(message);I think crypto is not supported as it's a heavy operation?
-
@oskar That's great thank you! I just managed to separate the logic to different files so it's shaping up well. Thanks for the help
-
Hi! You can find an implementation of SHA-256 here:
-
@Emil Thanks for the reply, the code lines I added in my post are what I found for vanilla implementation of cryptography, but it fails
-
@beachb_sl Cryptographic functions are not part of the javascript language itself, but typically included in browser APIs or e.g. Node.js environments.
To use SHA-256 encryption with the Hub SDK, use a pure javascript implementation. You should be able to find that with a simple online search.
If you want to run TypeScript, my suggestion is to transpile it to javascript first.