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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title> - jsFiddle demo by russau</title>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.6.4.js'></script>
<link rel="stylesheet" type="text/css" href="/css/normalize.css">
<link rel="stylesheet" type="text/css" href="/css/result-light.css">
<link rel="stylesheet" type="text/css" href="http://twitter.github.com/bootstrap/1.3.0/bootstrap.css">
<script type='text/javascript' src="https://github.com/Caligatio/jsSHA/raw/master/src/sha.js"></script>
<style type='text/css'>
body { padding-top: 60px; }
.container-fluid { min-width: 100px }
</style>
<script type='text/javascript'>//<![CDATA[
function dec2hex(s) { return (s < 15.5 ? '0' : '') + Math.round(s).toString(16); }
function hex2dec(s) { return parseInt(s, 16); }
function base32tohex(base32) {
var base32chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
var bits = "";
var hex = "";
for (var i = 0; i < base32.length; i++) {
var val = base32chars.indexOf(base32.charAt(i).toUpperCase());
bits += leftpad(val.toString(2), 5, '0');
}
for (var i = 0; i + 4 <= bits.length; i += 4) {
var chunk = bits.substr(i, 4);
hex = hex + parseInt(chunk, 2).toString(16);
}
return hex;
}
function leftpad(str, len, pad) {
if (len + 1 >= str.length) {
str = Array(len + 1 - str.length).join(pad) + str;
}
return str;
}
function updateOtp() {
var key = base32tohex($('#secret').val());
//var date = new Date();
var date = new Date(2013, 5, 12, 18, 0, 0, 0); //12 juin 2013 18h00
var epoch = Math.round(date.getTime() / 1000.0);
var time = leftpad(dec2hex(Math.floor(epoch / 30)), 16, '0');
var hmacObj = new jsSHA(time, 'HEX');
var hmac = hmacObj.getHMAC(key, 'HEX', 'SHA-1', "HEX");
$('#qrImg').attr('src', 'https://chart.googleapis.com/chart?chs=200x200&cht=qr&chl=200x200&chld=M|0&cht=qr&chl=otpauth://totp/user@host.com%3Fsecret%3D' + $('#secret').val());
$('#secretHex').text(key);
$('#secretHexLength').text((key.length * 4) + ' bits');
$('#epoch').text(time);
$('#hmac').empty();
if (hmac == 'KEY MUST BE IN BYTE INCREMENTS') {
$('#hmac').append($('<span/>').addClass('label important').append(hmac));
} else {
var offset = hex2dec(hmac.substring(hmac.length - 1));
var part1 = hmac.substr(0, offset * 2);
var part2 = hmac.substr(offset * 2, 8);
var part3 = hmac.substr(offset * 2 + 8, hmac.length - offset);
if (part1.length > 0) $('#hmac').append($('<span/>').addClass('label').append(part1));
$('#hmac').append($('<span/>').addClass('label success').append(part2));
if (part3.length > 0) $('#hmac').append($('<span/>').addClass('label').append(part3));
}
var otp = (hex2dec(hmac.substr(offset * 2, 8)) & hex2dec('7fffffff')) + '';
otp = (otp).substr(otp.length - 6, 6);
$('#otp').text(otp);
var a = otp;
}
function timer() {
var epoch = Math.round(new Date().getTime() / 1000.0);
var countDown = 30 - (epoch % 30);
if (epoch % 30 == 0) updateOtp();
$('#updatingIn').text(countDown);
}
$(function () {
updateOtp();
$('#update').click(function (event) {
updateOtp();
event.preventDefault();
});
$('#secret').keyup(function () {
updateOtp();
});
setInterval(timer, 1000);
});
//]]>
</script>
</head>
<body>
<div class="container-fluid">
<div>
<div class="row">
<div class="span8">
<h1>Time-based One-time Password Algorithm</h1>
<p>This page contains a javascript implementation of the <em>Time-based One-time Password Algorithm</em> used by Google Authenticator and described in the <a href="http://tools.ietf.org/id/draft-mraihi-totp-timebased-06.html">TOTP RFC Draft</a>.</p>
<p>Install Google Authenticator on your smartphone: <a href="http://itunes.apple.com/au/app/google-authenticator/id388497605?mt=8">iOS</a>, <a href="https://market.android.com/details?id=com.google.android.apps.authenticator&hl=en">Android</a>, <a href="http://m.google.com/authenticator">Blackberry</a>. As the TOTP is an open standard you can use this app to create one-time passwords for your own application. You add an account plus secret by scanning a QR code (more info on the <a href="http://code.google.com/p/google-authenticator/wiki/KeyUriFormat">google code wiki</a>). The javascript below implements the algorithm the smartphone app uses to generate the OTP - you would use this same algorithm <em>server-side</em> to verify an OTP.</p>
<p>Put it to the test by setting the base32 secret, scanning the QR code in Google Authenticate. You should see the same OTP on your smartphone and displayed at the bottom on the page.</p>
</div>
</div>
<div class="row">
<form>
<fieldset>
<div class="clearfix">
<label for="secret">Secret (base32)</label>
<div class="input"><input type="text" size="30" name="secret" id="secret" class="xlarge" value="JBSWY3DPEHPK3PXP" /></div>
</div>
<!-- /clearfix -->
<div class="clearfix">
<label>Secret (hex)</label>
<div class="input"><span class="label" id="secretHex"></span>
<span id='secretHexLength'></span></div>
</div>
<!-- /clearfix -->
<div class="clearfix">
<label>QR Code</label>
<div class="input"><img id="qrImg" src="" /></div>
</div>
<!-- /clearfix -->
<div class="clearfix">
<label>Unix epoch div 30 (padded hex)</label>
<div class="input"><span class="label" id='epoch'></span></div>
</div>
<!-- /clearfix -->
<div class="clearfix">
<label>HMAC(secret, time)</label>
<div class="input" id='hmac'></div>
</div>
<!-- /clearfix -->
<div class="clearfix">
<label>One-time Password</label>
<div class="input"><span class="label success" id='otp'></span></div>
</div>
<!-- /clearfix -->
<div class="clearfix">
<label>Updating in</label>
<div class="input"><span id='updatingIn'></span></div>
</div>
<!-- /clearfix -->
</fieldset>
</form>
</div>
</div>
</div>
</body>
</html>
|