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
|
//------------------------------------------------------------------------------------
// <copyright file="CounterBasedOtpGeneratorTests.cs" company="Stephen Jennings">
// Copyright 2011 Stephen Jennings. Licensed under the Apache License, Version 2.0.
// </copyright>
//------------------------------------------------------------------------------------
namespace OathNet.Test
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Moq;
using NUnit.Framework;
using OathNet;
public class CounterBasedOtpGeneratorTests
{
[Test]
public void GenerateOtp_without_hmac_returns_SHA1_with_bytearray_key()
{
var keyData = new byte[]
{
0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30,
0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30
};
var key = new Key(keyData);
this.TestSHA1AndAssert(key, 6, 0, this.GetOtpWithImplicitHMAC(key, 6, 0));
this.TestSHA1AndAssert(key, 6, 1, this.GetOtpWithImplicitHMAC(key, 6, 1));
this.TestSHA1AndAssert(key, 6, 2, this.GetOtpWithImplicitHMAC(key, 6, 2));
this.TestSHA1AndAssert(key, 6, 3, this.GetOtpWithImplicitHMAC(key, 6, 3));
this.TestSHA1AndAssert(key, 6, 4, this.GetOtpWithImplicitHMAC(key, 6, 4));
this.TestSHA1AndAssert(key, 6, 5, this.GetOtpWithImplicitHMAC(key, 6, 5));
this.TestSHA1AndAssert(key, 6, 6, this.GetOtpWithImplicitHMAC(key, 6, 6));
this.TestSHA1AndAssert(key, 6, 7, this.GetOtpWithImplicitHMAC(key, 6, 7));
this.TestSHA1AndAssert(key, 6, 8, this.GetOtpWithImplicitHMAC(key, 6, 8));
this.TestSHA1AndAssert(key, 6, 9, this.GetOtpWithImplicitHMAC(key, 6, 9));
}
[Test]
public void GenerateOtp_returns_SHA1_reference_values_with_bytearray_key()
{
var keyData = new byte[]
{
0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30,
0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30
};
var key = new Key(keyData);
this.TestSHA1AndAssert(key, 6, 0, "755224");
this.TestSHA1AndAssert(key, 6, 1, "287082");
this.TestSHA1AndAssert(key, 6, 2, "359152");
this.TestSHA1AndAssert(key, 6, 3, "969429");
this.TestSHA1AndAssert(key, 6, 4, "338314");
this.TestSHA1AndAssert(key, 6, 5, "254676");
this.TestSHA1AndAssert(key, 6, 6, "287922");
this.TestSHA1AndAssert(key, 6, 7, "162583");
this.TestSHA1AndAssert(key, 6, 8, "399871");
this.TestSHA1AndAssert(key, 6, 9, "520489");
}
[Test]
public void GenerateOtp_test_with_Google_Authenticator_1()
{
var keyData = new byte[] // Base-32: 32W3532IMVWGY3ZB
{
0xDE, 0xAD, 0xBE, 0xEF, 0x48,
0x65, 0x6C, 0x6C, 0x6F, 0x21
};
var key = new Key(keyData);
this.TestSHA1AndAssert(key, 6, 1, "092093");
this.TestSHA1AndAssert(key, 6, 11, "266262");
}
private string GetOtpWithImplicitHMAC(Key key, int digits, int counter)
{
var otp = new CounterBasedOtpGenerator(key, digits);
return otp.GenerateOtp(counter);
}
private void TestSHA1AndAssert(Key key, int digits, int counter, string expected)
{
var otp = new CounterBasedOtpGenerator(key, digits, new SHA1HMACAlgorithm());
var result = otp.GenerateOtp(counter);
Assert.AreEqual(expected, result);
}
}
}
|