-
Notifications
You must be signed in to change notification settings - Fork 1
/
paper-token.pl
152 lines (126 loc) · 4.01 KB
/
paper-token.pl
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/perl -w
#
# paper token is a software to generate paper-based OTP
#
# Copyright (C) 2010 Alexandre Dulaunoy, http://www.foo.be/
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
use Authen::HOTP qw(hotp);
use PDF::API2;
use PDF::Table;
use Data::Dumper;
use Getopt::Compact;
# default value is the test vector from RFC 4226
my $secret = "3132333435363738393031323334353637383930";
# starting counter
my $counter = 0;
# ending counter
my $end = 500;
my $digits = 6;
my @tokentable = [];
# window value (shown on one line and window of accepted token)
my $window = 14;
my $when = localtime();
my $opt = new Getopt::Compact(
name => 'paper token - http://www.quuxlabs.com/?p=431',
modes => '',
version => '0.1',
struct => [
[
[qw(s secret)],
qq(secret of the token in hex format - default is RFC 4226 test vector),
':s'
],
[ [qw(o output)], qq(output filename), ':s' ],
[ [qw(c counter)], qq(starting counter - default is 0), ':i' ],
[ [qw(e end)], qq(ending counter - default is 500), ':i' ],
[
[qw(w window)],
qq(window of authentication (one line) - default is 14), ':i'
],
[
[qw(d digits)],
qq(digits showed per OTP value - default is 6 - dec31.6), ':i'
],
]
);
my $opts = $opt->opts;
if ( !( defined( $opts->{output} ) ) ) {
print $opt->usage;
exit();
}
if ( defined( $opts->{counter} ) ) {
$counter = $opts->{counter};
}
if ( defined( $opts->{end} ) ) {
$end = $opts->{end};
}
if ( defined( $opts->{window} ) ) {
$window = $opts->{window};
}
if ( defined( $opts->{secret} ) ) {
$secret = $opts->{secret};
}
my $sn = substr( $secret, 0, 10 );
if ( defined( $opts->{digits} ) ) {
$digits = $opts->{digits};
}
for ( $count = $counter ; $count < $counter + $end ; $count = $count + $window )
{
my @tj;
for ( $j = $count ; $j < $count + $window ; $j = $j + 1 ) {
my $pass = hotp( $secret, $j, $digits );
push( @tj, "$pass" );
}
@val = ["@tj"];
push( @{ $tokentable[0] }, @val );
}
my $pdftable = new PDF::Table;
my $pdf = new PDF::API2( -file => $opts->{output} );
my $page = $pdf->page;
my $gfx = $page->gfx;
my $tokeninfo = "Paper token for HOTP token S/N : " . $sn;
my $tokenhowto =
"How to use? Read from left to right and when you use a token, strikethrough the used value.";
my $tokenadv =
"Generated by paper token (http://www.quuxlabs.com/?p=431) at " . $when;
$pdf->info(
'Author' => "Alexandre Dulaunoy - http://www.foo.be/",
'Creator' => "Paper Token - http://www.quuxlabs.com/?p=431",
'Producer' => "PDF::API2",
'Title' => $tokeninfo,
);
$fnt = $pdf->corefont('Helvetica-Bold');
$fntlow = $pdf->corefont('Helvetica');
$gfx->textlabel( 20, 750, $fnt, 11, $tokeninfo );
$gfx->textlabel( 20, 739, $fntlow, 11, $tokenhowto );
$gfx->textlabel( 20, 728, $fntlow, 11, $tokenadv );
$pdftable->table(
$pdf,
$page,
@tokentable,
-x => 20,
-w => 40,
-start_y => 700,
-next_y => 700,
-start_h => 700,
-next_h => 700,
-w => 600,
-padding => 2,
-padding_right => 1,
-background_color_odd => "lightgray",
-background_color_even => "gray",
font => $pdf->corefont( "Helvetica", -encoding => "utf8" ),
font_size => 11,
border => 0
);
$pdf->save();