seckit 1.0.5
seckit: ^1.0.5 copied to clipboard
Secure cryptographic utilities for Dart JWT authentication, field encryption, password hashing, and deterministic hashing.
// ignore_for_file: avoid_print
import 'dart:convert';
import 'package:seckit/seckit.dart';
/// Complete example demonstrating all seckit features with security best practices.
void main() {
print('=== Seckit Package Demo ===\n');
// π Configuration (use environment variables in production!)
final config = _createConfig();
// 1οΈβ£ JWT Authentication
_demonstrateJWT(config);
// 2οΈβ£ Field Encryption (searchable, HMAC-authenticated)
_demonstrateFieldEncryption(config);
// 3οΈβ£ Password Hashing (bcrypt)
_demonstratePasswordHashing();
// 4οΈβ£ Deterministic Hashing (for database lookups)
_demonstrateDeterministicHashing(config);
// 5οΈβ£ Email Utilities
_demonstrateEmailUtils();
}
Config _createConfig() {
// β οΈ PRODUCTION: Load from environment variables!
// final secretKey = Platform.environment['JWT_SECRET']!;
// final dbSecretKey = Platform.environment['DB_SECRET']!;
final dbKey = base64.encode(List<int>.generate(32, (i) => i % 256));
return Config(
secretKey: 'my-super-secret-jwt-key-32chars!',
dbSecretKey: dbKey,
devAuthToken: 'dev-token-for-testing-only-123',
isProd: false, // Set true in production!
);
}
void _demonstrateJWT(Config config) {
print('π JWT Token Generation & Validation');
final jwtHandler = JwtHandler(
secretKey: config.secretKey,
devAuthToken: config.devAuthToken,
isProd: config.isProd,
userIdKey: 'user_id',
);
// Generate token with custom claims
final token = jwtHandler.generateToken(
claims: {'user_id': 123, 'role': 'admin'},
maxAge: Duration(hours: 1),
);
print('β Token: ${token.substring(0, 50)}...');
// Validate token
final result = jwtHandler.validateToken(token);
print('β Valid: ${result.isValue}');
// Extract user ID
final userIdResult = jwtHandler.getUserIdFromToken(token);
if (userIdResult.isValue) {
print('β User ID: ${userIdResult.asValue!.value}\n');
}
}
void _demonstrateFieldEncryption(Config config) {
print('π Field Encryption (AES-256-CBC + HMAC)');
final encryptor = FieldEncryptor(
dbSecretKey: config.dbSecretKey,
salt: 'prod-salt-16chars',
);
// Encrypt sensitive data
const email = 'user@example.com';
final encrypted = encryptor.encrypt(email).asValue!.value;
print('β Original: $email');
print('β Encrypted: ${encrypted.substring(0, 40)}...');
// Decrypt
final decrypted = encryptor.decrypt(encrypted).asValue!.value;
print('β Decrypted: $decrypted');
print('β Match: ${email == decrypted}');
print('β HMAC: Authenticated (tampering protected)\n');
}
void _demonstratePasswordHashing() {
print('π Password Hashing (bcrypt)');
const hasher = PasswordHasher();
const password = 'MySecurePass123!';
// Hash password
final hash = hasher.hash(password).asValue!.value;
print('β Password: $password');
print('β Hash: ${hash.substring(0, 30)}...');
// Verify correct password
final valid = hasher.verify(password, hash).asValue!.value;
print('β Correct password: $valid');
// Verify wrong password
final invalid = hasher.verify('WrongPass', hash).asValue!.value;
print('β Wrong password: $invalid\n');
}
void _demonstrateDeterministicHashing(Config config) {
print('π Deterministic Hashing (HMAC-SHA256)');
final hasher = DeterministicHasher(
secretKey: config.secretKey,
salt: 'search-salt-16ch',
);
// Hash for database search
const email = 'john.doe@company.com';
final hash1 = hasher.hash(email).asValue!.value;
final hash2 = hasher.hash(email).asValue!.value;
print('β Email: $email');
print('β Hash 1: ${hash1.substring(0, 30)}...');
print('β Hash 2: ${hash2.substring(0, 30)}...');
print('β Deterministic: ${hash1 == hash2}');
// Verify hash
final match = hasher.verify(email, hash1).asValue!.value;
print('β Verification: $match\n');
}
void _demonstrateEmailUtils() {
print('π§ Email Masking');
final examples = [
'john@example.com',
'a@test.com',
'long.email.address@company.co.uk',
];
for (final email in examples) {
final masked = EmailUtils.mask(email);
print('β $email β $masked');
}
print('\n=== Demo Complete! ===');
print('π See SECURITY.md for best practices');
print('π All operations use constant-time comparisons');
print('β¨ HMAC authentication prevents tampering');
}