You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: examples/official-site/sqlpage/migrations/67_hmac_function.sql
+66-33Lines changed: 66 additions & 33 deletions
Original file line number
Diff line number
Diff line change
@@ -10,63 +10,96 @@ VALUES (
10
10
'hmac',
11
11
'0.38.0',
12
12
'shield-lock',
13
-
'Computes the [HMAC](https://en.wikipedia.org/wiki/HMAC) (Hash-based Message Authentication Code) of the input data using a secret keyand a cryptographic hash function.
13
+
'Creates a unique "signature" for your data using a secret key. This signature proves that the data hasn''t been tampered with and comes from someone who knows the secret.
14
14
15
-
HMAC is used to verify both the data integrity and authenticity of a message. It is commonly used for:
16
-
- Generating secure tokens and signatures
17
-
- API request authentication
18
-
- Webhook signature verification
19
-
- Data integrity validation
15
+
Think of it like a wax seal on a letter - only someone with the right seal (your secret key) can create it, and if someone changes the letter, the seal won''t match anymore.
20
16
21
-
### Example
17
+
### What is HMAC used for?
22
18
23
-
#### Generate an HMAC for API authentication
19
+
**HMAC** (Hash-based Message Authentication Code) is commonly used to:
20
+
- **Verify webhooks**: Check that notifications from services like Shopify, Stripe, or GitHub are genuine
21
+
- **Secure API requests**: Prove that an API request comes from an authorized source
22
+
- **Generate secure tokens**: Create temporary access codes for downloads or password resets
23
+
- **Protect data**: Ensure data hasn''t been modified during transmission
24
24
25
-
```sql
26
-
-- Generate a secure signature for an API request
27
-
SELECT sqlpage.hmac(
28
-
''user_id=123&action=update'',
29
-
''my-secret-api-key'',
30
-
''sha256''
31
-
) as request_signature;
32
-
```
25
+
### How to use it
33
26
34
-
#### Verify a webhook signature
27
+
The `sqlpage.hmac` function takes three inputs:
28
+
1. **Your data** - The text you want to sign (like a message or request body)
29
+
2. **Your secret key** - A password only you know (keep this safe!)
30
+
3. **Algorithm** (optional) - Either `sha256` (default) or `sha512`
31
+
32
+
It returns a long string of letters and numbers (the signature). If someone changes even one letter in your data, the signature will be completely different.
33
+
34
+
### Example 1: Verify Shopify Webhooks
35
+
36
+
When Shopify sends you a webhook (like when someone places an order), it includes a signature. Here''s how to verify it''s really from Shopify:
35
37
36
38
```sql
37
-
-- Verify that a webhook request is authentic
39
+
-- Shopify includes the signature in the X-Shopify-Hmac-SHA256 header
- The function returns a hexadecimal string representation of the HMAC.
65
-
- If either `data` or `key` is NULL, the function returns NULL.
66
-
- The `algorithm` parameter is optional and defaults to `sha256` if not specified.
67
-
- Supported algorithms: `sha256`, `sha512`.
68
-
- The key can be of any length. For maximum security, use a key that is at least as long as the hash output (32 bytes for SHA-256, 64 bytes for SHA-512).
69
-
- Keep your secret keys secure and never expose them in client-side code or version control.
98
+
- **Keep your secret key safe**: Store it in environment variables using `sqlpage.environment_variable()`, never hardcode it in your SQL files
99
+
- **Use strong keys**: Your secret should be long and random (at least 32 characters)
100
+
- **The signature is case-sensitive**: Even one wrong letter means the signature won''t match
101
+
- **Algorithms**: Use `sha256` for most cases (it''s the default), or `sha512` for extra security
102
+
- **NULL handling**: If your data or key is NULL, the function returns NULL
0 commit comments