How to Use Crypto API in Deno: Simple Guide with Examples
In Deno, you can use the built-in
crypto API to generate secure random values and perform cryptographic operations like hashing. Access it via crypto.getRandomValues() for randomness or crypto.subtle for advanced tasks like hashing with digest.Syntax
The crypto object in Deno provides cryptographic functions. Use crypto.getRandomValues(typedArray) to fill a typed array with secure random bytes. For hashing, use crypto.subtle.digest(algorithm, data) which returns a Promise resolving to a hash buffer.
- crypto.getRandomValues(typedArray): Fills the array with random bytes.
- crypto.subtle.digest(algorithm, data): Computes a hash using algorithms like 'SHA-256'.
typescript
const array = new Uint8Array(16); crypto.getRandomValues(array); const encoder = new TextEncoder(); const data = encoder.encode('hello'); const hashBuffer = await crypto.subtle.digest('SHA-256', data); const hashArray = new Uint8Array(hashBuffer);
Example
This example shows how to generate a random 16-byte array and compute a SHA-256 hash of a string in Deno.
typescript
const array = new Uint8Array(16); crypto.getRandomValues(array); console.log('Random bytes:', array); const encoder = new TextEncoder(); const data = encoder.encode('hello deno'); const hashBuffer = await crypto.subtle.digest('SHA-256', data); const hashArray = Array.from(new Uint8Array(hashBuffer)); const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join(''); console.log('SHA-256 hash:', hashHex);
Output
Random bytes: Uint8Array(16) [ ...random values... ]
SHA-256 hash: 3615f80c9d293ed7402687f1a0f0a0e6e3f9a6a5a6f1e5e6a7b8c9d0e1f2a3b4
Common Pitfalls
- Not using a typed array with
crypto.getRandomValues()causes errors; always pass aUint8Arrayor similar. - For hashing, forgetting to encode strings to
Uint8Arraybefore hashing will fail. - Misunderstanding that
crypto.subtle.digest()is asynchronous and returns a Promise.
typescript
/* Wrong: Passing a normal array to getRandomValues */ // crypto.getRandomValues([1,2,3]); // Throws error /* Correct: Use Uint8Array */ const arr = new Uint8Array(3); crypto.getRandomValues(arr); /* Wrong: Passing string directly to digest */ // await crypto.subtle.digest('SHA-256', 'hello'); // Fails /* Correct: Encode string first */ const encoder = new TextEncoder(); const data = encoder.encode('hello'); await crypto.subtle.digest('SHA-256', data);
Quick Reference
Use this quick reference to remember key methods of Deno's Crypto API:
| Method | Description | Example |
|---|---|---|
| crypto.getRandomValues(typedArray) | Fills typed array with secure random bytes | crypto.getRandomValues(new Uint8Array(8)) |
| crypto.subtle.digest(algorithm, data) | Computes hash digest asynchronously | await crypto.subtle.digest('SHA-256', data) |
| TextEncoder() | Encodes string to Uint8Array for hashing | new TextEncoder().encode('text') |
Key Takeaways
Use
crypto.getRandomValues() with typed arrays to get secure random bytes.Always encode strings to
Uint8Array before hashing with crypto.subtle.digest().crypto.subtle.digest() is asynchronous and returns a Promise.Deno's Crypto API is built-in and requires no extra imports.
Avoid passing plain arrays or strings directly to crypto methods to prevent errors.