ValaOtp/t/rfc.vala

88 lines
3.2 KiB
Vala
Raw Normal View History

2022-01-23 19:01:07 +01:00
/* rfc.vala
*
* Written in 2022 by Lucas
*
* To the extent possible under law, the author(s) have dedicated all
* copyright and related and neighboring rights to this software to the
* public domain worldwide. This software is distributed without any
* warranty.
*
* You should have received a copy of the CC0 Public Domain Dedication
* along with this software. If not, see
* <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
unowned string RFC_4226_KEY = "12345678901234567890";
unowned string RFC_6238_SHA1_KEY = "12345678901234567890";
unowned string RFC_6238_SHA256_KEY = "12345678901234567890123456789012";
unowned string RFC_6238_SHA512_KEY = "123456789012345678901234567890" +
"1234567890123456789012345678901234";
void add_rfc_4226_test_vectors() {
Test.add_func("/otp/rfc4226", () => {
Otp.Hotp hotp = new Otp.Hotp(RFC_4226_KEY.data);
assert(hotp.get_value_at(0) == "755224");
assert(hotp.get_value_at(1) == "287082");
assert(hotp.get_value_at(2) == "359152");
assert(hotp.get_value_at(3) == "969429");
assert(hotp.get_value_at(4) == "338314");
assert(hotp.get_value_at(5) == "254676");
assert(hotp.get_value_at(6) == "287922");
assert(hotp.get_value_at(7) == "162583");
assert(hotp.get_value_at(8) == "399871");
assert(hotp.get_value_at(9) == "520489");
});
}
void add_rfc_6238_sha1_test_vectors() {
Test.add_func("/otp/rfc6238/sha1", () => {
Otp.Totp totp = new Otp.Totp(RFC_6238_SHA1_KEY.data, 30, 8,
ChecksumType.SHA1);
assert(totp.get_value_at_timestamp(59) == "94287082");
assert(totp.get_value_at_timestamp(1111111109) == "07081804");
assert(totp.get_value_at_timestamp(1111111111) == "14050471");
assert(totp.get_value_at_timestamp(1234567890) == "89005924");
assert(totp.get_value_at_timestamp(2000000000) == "69279037");
assert(totp.get_value_at_timestamp(20000000000) == "65353130");
});
}
void add_rfc_6238_sha256_test_vectors() {
Test.add_func("/otp/rfc6238/sha256", () => {
Otp.Totp totp = new Otp.Totp(RFC_6238_SHA256_KEY.data, 30, 8,
ChecksumType.SHA256);
assert(totp.get_value_at_timestamp(59) == "46119246");
assert(totp.get_value_at_timestamp(1111111109) == "68084774");
assert(totp.get_value_at_timestamp(1111111111) == "67062674");
assert(totp.get_value_at_timestamp(1234567890) == "91819424");
assert(totp.get_value_at_timestamp(2000000000) == "90698825");
assert(totp.get_value_at_timestamp(20000000000) == "77737706");
});
}
void add_rfc_6238_sha512_test_vectors() {
Test.add_func("/otp/rfc6238/sha512", () => {
Otp.Totp totp = new Otp.Totp(RFC_6238_SHA512_KEY.data, 30, 8,
ChecksumType.SHA512);
assert(totp.get_value_at_timestamp(59) == "90693936");
assert(totp.get_value_at_timestamp(1111111109) == "25091201");
assert(totp.get_value_at_timestamp(1111111111) == "99943326");
assert(totp.get_value_at_timestamp(1234567890) == "93441116");
assert(totp.get_value_at_timestamp(2000000000) == "38618901");
assert(totp.get_value_at_timestamp(20000000000) == "47863826");
});
}
void main(string[] args) {
Test.init(ref args);
add_rfc_4226_test_vectors();
add_rfc_6238_sha1_test_vectors();
add_rfc_6238_sha256_test_vectors();
add_rfc_6238_sha512_test_vectors();
Test.run();
}