0
0
DenoHow-ToBeginner ยท 3 min read

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 a Uint8Array or similar.
  • For hashing, forgetting to encode strings to Uint8Array before 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:

MethodDescriptionExample
crypto.getRandomValues(typedArray)Fills typed array with secure random bytescrypto.getRandomValues(new Uint8Array(8))
crypto.subtle.digest(algorithm, data)Computes hash digest asynchronouslyawait crypto.subtle.digest('SHA-256', data)
TextEncoder()Encodes string to Uint8Array for hashingnew 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.