-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample-qr-generation.ts
More file actions
77 lines (65 loc) · 2.43 KB
/
Copy pathexample-qr-generation.ts
File metadata and controls
77 lines (65 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { QRCodeGenerator, ErrorCorrectionLevel } from './src/qr';
import { OtpAuth } from './src/qr';
import { TOTP } from './src/core/totp';
async function demonstrateQRCodeGeneration() {
console.log('=== QR Code Generation Demo ===\n');
// Generate a secure secret
const secret = TOTP.generateSecret(20);
console.log(`Generated secret: ${secret}\n`);
// Create TOTP configuration
const otpauthConfig = {
label: 'MyApp:user@example.com',
secret: secret,
issuer: 'MyApp',
algorithm: 'sha1' as const,
digits: 6 as 6 | 7 | 8,
period: 30
};
// Generate otpauth URI
const otpauthUri = OtpAuth.generateTOTPUri(otpauthConfig);
console.log(`otpauth URI: ${otpauthUri}\n`);
// Parse the URI back
const parsed = OtpAuth.parseUri(otpauthUri);
console.log('Parsed URI:');
console.log(` Type: ${parsed.type}`);
console.log(` Label: ${parsed.label}`);
console.log(` Secret: ${parsed.params.secret}`);
console.log(` Issuer: ${parsed.params.issuer}`);
console.log(` Digits: ${parsed.params.digits}`);
console.log(` Period: ${parsed.params.period}\n`);
// Generate QR code as data URI
console.log('Generating QR code...');
const qrOptions = {
otpauth: otpauthConfig,
width: 300,
margin: 4,
errorCorrectionLevel: ErrorCorrectionLevel.MEDIUM,
color: {
dark: '#000000',
light: '#FFFFFF'
}
};
try {
const qrCode = await QRCodeGenerator.generate(qrOptions);
console.log(`QR code generated successfully!`);
console.log(`Data URI length: ${qrCode.dataUri.length} characters`);
console.log(`Dimensions: ${qrCode.width}x${qrCode.height}px\n`);
// Generate QR code as SVG
console.log('Generating QR code as SVG...');
const svgQRCode = await QRCodeGenerator.generateSVG(qrOptions);
console.log(`SVG QR code generated successfully!`);
console.log(`SVG length: ${svgQRCode.length} characters\n`);
// Generate QR code for terminal
console.log('Generating QR code for terminal display...');
const terminalQRCode = await QRCodeGenerator.generateTerminal(qrOptions);
console.log('Terminal QR code:');
console.log(terminalQRCode);
// Calculate QR code version
const version = await QRCodeGenerator.calculateVersion(qrOptions);
console.log(`\nQR code version: ${version}`);
} catch (error) {
console.error('Error generating QR code:', error);
}
}
// Run the demonstration
demonstrateQRCodeGeneration().catch(console.error);