summaryrefslogtreecommitdiff
path: root/crypt.h
diff options
context:
space:
mode:
authorꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id>2022-09-14 18:19:14 +0800
committerꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id>2022-09-14 18:19:14 +0800
commit306cf74eb0101a12b51549866a4d60296618ee0b (patch)
tree89fe13b234f40f97de84cdb6610bcd11e1d47a88 /crypt.h
OAuth part
The minimum to pass all authentications and arrive at the embedded app index. This library is to be used with shopify-app-template-c for now, as it assumes the existence of shopify.app.toml in the parent directory, and index.html in the frontend directory.
Diffstat (limited to 'crypt.h')
-rw-r--r--crypt.h34
1 files changed, 34 insertions, 0 deletions
diff --git a/crypt.h b/crypt.h
new file mode 100644
index 0000000..f836bcc
--- /dev/null
+++ b/crypt.h
@@ -0,0 +1,34 @@
+#include <gcrypt.h>
+
+static inline void crypt_init()
+{
+ gcry_check_version("1.9.4");
+}
+
+static inline bool crypt_maccmp(const char *key, const char *query,
+ const char *hmac)
+{
+ gcry_mac_hd_t hd;
+ gcry_mac_open(&hd, GCRY_MAC_HMAC_SHA256, GCRY_MAC_FLAG_SECURE, NULL);
+ gcry_mac_setkey(hd, key, strlen(key));
+ gcry_mac_write(hd, query, strlen(query));
+ size_t hmac_sha256_len = 32;
+ unsigned char hmac_sha256[hmac_sha256_len + 1];
+ gcry_mac_read(hd, hmac_sha256, &hmac_sha256_len);
+ gcry_mac_close(hd);
+ char hmac_sha256_str[65] = { [0] = '\0' };
+ for (int i = 0; i < hmac_sha256_len; i++)
+ sprintf(hmac_sha256_str, "%s%02x", hmac_sha256_str,
+ hmac_sha256[i]);
+ return !strcmp(hmac, hmac_sha256_str);
+}
+
+static inline void crypt_getnonce(char *string, const size_t string_len)
+{
+ string[0] = '\0';
+ const size_t nonce_len = string_len / 2;
+ unsigned char nonce[nonce_len + 1];
+ gcry_create_nonce(nonce, nonce_len);
+ for (int i = 0; i < nonce_len; i++)
+ sprintf(string, "%s%02x", string, nonce[i]);
+}