Add Yandex OTP support

This commit is contained in:
Mikhail Prokofev 2022-01-07 12:49:53 +03:00
parent e54ac9aba4
commit af2bf6f683
15 changed files with 541 additions and 61 deletions

View file

@ -0,0 +1,53 @@
package com.beemdevelopment.aegis.crypto.otp;
import static org.junit.Assert.assertEquals;
import com.beemdevelopment.aegis.crypto.CryptoUtils;
import com.beemdevelopment.aegis.encoding.Base32;
import com.beemdevelopment.aegis.encoding.EncodingException;
import org.junit.Test;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
public class YAOTPTest {
private static final Vector[] TEST_CASES = new Vector[]{
new Vector("5239", "6SB2IKNM6OBZPAVBVTOHDKS4FAAAAAAADFUTQMBTRY", 1641559648L, "umozdicq"),
new Vector("7586", "LA2V6KMCGYMWWVEW64RNP3JA3IAAAAAAHTSG4HRZPI", 1581064020L, "oactmacq"),
new Vector("7586", "LA2V6KMCGYMWWVEW64RNP3JA3IAAAAAAHTSG4HRZPI", 1581090810L, "wemdwrix"),
new Vector("5210481216086702", "JBGSAU4G7IEZG6OY4UAXX62JU4AAAAAAHTSG4HXU3M", 1581091469L, "dfrpywob"),
new Vector("5210481216086702", "JBGSAU4G7IEZG6OY4UAXX62JU4AAAAAAHTSG4HXU3M", 1581093059L, "vunyprpd"),
};
@Test
public void validateYaOtp() throws InvalidKeyException, NoSuchAlgorithmException, IOException {
for (Vector testCase : TEST_CASES) {
YAOTP otp = YAOTP.generateOTP(
Base32.decode(testCase.secret.substring(0, 26)),
CryptoUtils.toBytes(testCase.pin.toCharArray()),
8,
"HmacSHA256",
testCase.timestamp,
30
);
assertEquals(testCase.expected, otp.toString());
}
}
public static class Vector {
public String pin;
public String secret;
public long timestamp;
public String expected;
public Vector(String pin, String secret, long timestamp, String expected) {
this.pin = pin;
this.secret = secret;
this.timestamp = timestamp;
this.expected = expected;
}
}
}

View file

@ -0,0 +1,35 @@
package com.beemdevelopment.aegis.util;
import static org.junit.Assert.assertThrows;
import com.beemdevelopment.aegis.encoding.Base32;
import com.beemdevelopment.aegis.encoding.EncodingException;
import com.beemdevelopment.aegis.otp.OtpInfoException;
import org.junit.Test;
public class YandexUtilsTest {
private static final String[] vectors = new String[]{
"LA2V6KMCGYMWWVEW64RNP3JA3IAAAAAAHTSG4HRZPI", // correct
"LA2V6KMCGYMWWVEW64RNP3JA3I", // secret from QR - no validation
"AA2V6KMCGYMWWVEW64RNP3JA3IAAAAAAHTSG4HRZPI", // first letter is different
"AA2V6KMCGJA3IAAAAAAHTSG4HRZPI" // size is wrong
};
@Test(expected = Test.None.class)
public void testValidationOk() throws EncodingException, OtpInfoException {
YandexUtils.validateSecret(getBase32Vector(0));
YandexUtils.validateSecret(getBase32Vector(1));
}
@Test
public void testYandexSecretValidation() {
assertThrows(OtpInfoException.class, () -> YandexUtils.validateSecret(getBase32Vector(2)));
assertThrows(OtpInfoException.class, () -> YandexUtils.validateSecret(getBase32Vector(3)));
}
private byte[] getBase32Vector(int vectorIndex) throws EncodingException {
return Base32.decode(vectors[vectorIndex]);
}
}