1const std = @import("std");
2const totp = @import("totp");
3const expect = @import("expect").expect;
4const extras = @import("extras");
5const from_hex = extras.from_hex;
6
7test {
8 try expect(&totp.hotp(6, &.{ 0x1f, 0x86, 0x98, 0x69, 0x0e, 0x02, 0xca, 0x16, 0x61, 0x85, 0x50, 0xef, 0x7f, 0x19, 0xda, 0x8e, 0x94, 0x5b, 0x55, 0x5a })).toEqualString("872921");
9}
10
11const seed = "3132333435363738393031323334353637383930";
12const Sha1 = std.crypto.hash.Sha1;
13// zig fmt: off
14test { try expect(&totp.totp(8, Sha1, 0, 30, &from_hex(seed), 59)).toEqualString("94287082"); }
15test { try expect(&totp.totp(8, Sha1, 0, 30, &from_hex(seed), 1111111109)).toEqualString("07081804"); }
16test { try expect(&totp.totp(8, Sha1, 0, 30, &from_hex(seed), 1111111111)).toEqualString("14050471"); }
17test { try expect(&totp.totp(8, Sha1, 0, 30, &from_hex(seed), 1234567890)).toEqualString("89005924"); }
18test { try expect(&totp.totp(8, Sha1, 0, 30, &from_hex(seed), 2000000000)).toEqualString("69279037"); }
19test { try expect(&totp.totp(8, Sha1, 0, 30, &from_hex(seed), 20000000000)).toEqualString("65353130"); }
20// zig fmt: on
21
22const seed32 = "3132333435363738393031323334353637383930313233343536373839303132";
23const Sha256 = std.crypto.hash.sha2.Sha256;
24// zig fmt: off
25test { try expect(&totp.totp(8, Sha256, 0, 30, &from_hex(seed32), 59)).toEqualString("46119246"); }
26test { try expect(&totp.totp(8, Sha256, 0, 30, &from_hex(seed32), 1111111109)).toEqualString("68084774"); }
27test { try expect(&totp.totp(8, Sha256, 0, 30, &from_hex(seed32), 1111111111)).toEqualString("67062674"); }
28test { try expect(&totp.totp(8, Sha256, 0, 30, &from_hex(seed32), 1234567890)).toEqualString("91819424"); }
29test { try expect(&totp.totp(8, Sha256, 0, 30, &from_hex(seed32), 2000000000)).toEqualString("90698825"); }
30test { try expect(&totp.totp(8, Sha256, 0, 30, &from_hex(seed32), 20000000000)).toEqualString("77737706"); }
31// zig fmt: on
32
33const seed64 = "31323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334";
34const Sha512 = std.crypto.hash.sha2.Sha512;
35// zig fmt: off
36test { try expect(&totp.totp(8, Sha512, 0, 30, &from_hex(seed64), 59)).toEqualString("90693936"); }
37test { try expect(&totp.totp(8, Sha512, 0, 30, &from_hex(seed64), 1111111109)).toEqualString("25091201"); }
38test { try expect(&totp.totp(8, Sha512, 0, 30, &from_hex(seed64), 1111111111)).toEqualString("99943326"); }
39test { try expect(&totp.totp(8, Sha512, 0, 30, &from_hex(seed64), 1234567890)).toEqualString("93441116"); }
40test { try expect(&totp.totp(8, Sha512, 0, 30, &from_hex(seed64), 2000000000)).toEqualString("38618901"); }
41test { try expect(&totp.totp(8, Sha512, 0, 30, &from_hex(seed64), 20000000000)).toEqualString("47863826"); }
42// zig fmt: on
43
44test {
45 const allocator = std.testing.allocator;
46 const url = try totp.generateUrl(allocator, "ACME Co", "john.doe@email.com", &from_hex("3dc6caa4824a6d288767b2331e20b43166cb85d9"), .SHA1, 6, 30);
47 defer allocator.free(url);
48 try expect(url).toEqualString("otpauth://totp/ACME%20Co:john.doe@email.com?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ&algorithm=SHA1&digits=6&period=30&issuer=ACME%20Co");
49}
50test {
51 const allocator = std.testing.allocator;
52 const url = try totp.generateUrl(allocator, "Example", "alice@google.com", &from_hex("48656c6c6f21deadbeef"), .SHA1, 6, 30);
53 defer allocator.free(url);
54 try expect(url).toEqualString("otpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&algorithm=SHA1&digits=6&period=30&issuer=Example");
55}
56test {
57 const allocator = std.testing.allocator;
58 const url = try totp.generateUrl(allocator, "otpauth demo", "username@example.org", &from_hex("0000008421d6b5adef7bc6318ce739f7bdefffff"), .SHA1, 6, 30);
59 defer allocator.free(url);
60 try expect(url).toEqualString("otpauth://totp/otpauth%20demo:username@example.org?secret=AAAABBBB22223333YYYYZZZZ66667777&algorithm=SHA1&digits=6&period=30&issuer=otpauth%20demo");
61}
62
63test {
64 const b32 = "SC5IH4S3HJLEWOGXJM4BL6CH5BDSYHBV";
65 const hex_expected = "90ba83f25b3a564b38d74b3815f847e8472c1c35";
66 const raw_actual = try totp.decodeBase32(b32, std.crypto.hash.Sha1);
67 const hex_actual = extras.to_hex(raw_actual);
68 try expect(&hex_actual).toEqualString(hex_expected);
69}