secure_nonce 2.0.1 copy "secure_nonce: ^2.0.1" to clipboard
secure_nonce: ^2.0.1 copied to clipboard

A cryptographically secure nonce generator for Dart. Generate random nonces as hex, bytes, base64, or alphanumeric strings using cryptographically secure random number generation.

example/secure_nonce_example.dart

// ignore_for_file: avoid_print
library;

import 'package:secure_nonce/secure_nonce.dart';

void main() {
  final nonce = SecureNonce();

  print('secure_nonce examples\n');

  // Basic hex nonce - most common use case
  print('Hex nonces:');
  final hex16 = nonce.generate(16);
  final hex32 = nonce.generate(32);
  print('  16 bytes -> ${hex16.length} hex chars: $hex16');
  print('  32 bytes -> ${hex32.length} hex chars: $hex32');
  print('');

  // Raw bytes for crypto operations
  print('Raw bytes (for encryption/hashing):');
  final iv = nonce.generateBytes(16);
  print('  AES IV (16 bytes): $iv');
  print('');

  // Base64 for APIs and headers
  print('Base64 (for headers/cookies):');
  print('  Token (24 bytes): ${nonce.generateBase64(24)}');
  print('');

  // URL-safe base64 - no encoding needed in URLs
  print('URL-safe base64:');
  final token = nonce.generateBase64Url(24);
  print('  https://example.com/verify?token=$token');
  print('');

  // Alphanumeric for user-facing codes
  print('Alphanumeric (human-readable):');
  print('  Invite code: ${nonce.generateAlphanumeric(8)}');
  print('  Order ID: ${nonce.generateAlphanumeric(12)}');
}
1
likes
160
points
107
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

A cryptographically secure nonce generator for Dart. Generate random nonces as hex, bytes, base64, or alphanumeric strings using cryptographically secure random number generation.

Repository (GitHub)
View/report issues

License

MIT (license)

More

Packages that depend on secure_nonce