What This Adds In Cloud Code
- Built-in hash/HMAC helpers exposed in JS style.
- Predictable random token and UUID generation.
- Cipher helper with explicit algorithm/digest controls.
Quick Start
js
const crypto = require('crypto');
const digest = crypto.createHash('sha256').update('payload').digest('hex');API
| Function | Params | Returns | Notes |
|---|---|---|---|
secure_random(length) / secureRandom(length) | length?: number | string | Hex output. Default length: 64. |
uuid() | none | string | UUID string. |
createHash(algorithm) | algorithm: string | CryptoCalculator | Use update(...).digest(...). |
createHmac(algorithm, key) | algorithm: string, key: string | CryptoCalculator | Use for signatures/webhooks. |
createCipheriv(algorithm, key, iv) | strings | CryptoCalculator | Use update(...).final(...). |
Practical Examples
js
const crypto = require('crypto');
const signature = crypto
.createHmac('sha256', Nimbu.Site.env.get('WEBHOOK_SECRET'))
.update(request.rawBody || '')
.digest('hex');js
const crypto = require('crypto');
const token = crypto.secure_random(32); // `secureRandom` alias is also availableFailure Modes & Gotchas
- Unsupported algorithms/digests can return empty output.
- Keep keys in
Nimbu.Site.env, never hardcoded. - Compare signatures in constant-time logic when possible.