@Override public void setUp() throws Exception { KEYBYTES1 = Base32String.decode("7777777777777777"); KEYBYTES2 = Base32String.decode("22222222222222222"); mac1 = Mac.getInstance("HMACSHA1"); mac1.init(new SecretKeySpec(KEYBYTES1, "")); mac2 = Mac.getInstance("HMACSHA1"); mac2.init(new SecretKeySpec(KEYBYTES2, "")); passcodeGenerator1 = new PasscodeGenerator(mac1); passcodeGenerator2 = new PasscodeGenerator(mac2); signer = AccountDb.getSigningOracle("7777777777777777"); }
static String getCheckCode(String secret) throws GeneralSecurityException, DecodingException { final byte[] keyBytes = Base32String.decode(secret); Mac mac = Mac.getInstance("HMACSHA1"); mac.init(new SecretKeySpec(keyBytes, "")); PasscodeGenerator pcg = new PasscodeGenerator(mac); return pcg.generateResponseCode(0L); }
public void testRegressionGenerateResponseCode() throws Exception { // test with long assertEquals("724477", passcodeGenerator1.generateResponseCode(0L)); assertEquals("815107", passcodeGenerator1.generateResponseCode(123456789123456789L)); // test with byte[] for 0L and then for 123456789123456789L assertEquals( "724477", passcodeGenerator1.generateResponseCode(Base32String.decode("AAAAAAAAAAAAA"))); assertEquals( "815107", passcodeGenerator1.generateResponseCode(Base32String.decode("AG3JWS5M2BPRK"))); // test with long and byte[] assertEquals( "498157", passcodeGenerator1.generateResponseCode( 123456789123456789L, "challenge".getBytes("UTF-8"))); }
public void testGenerateResponseCodeLong() throws Exception { // test with long String response1Long = passcodeGenerator1.generateResponseCode(123456789123456789L); assertTrue(passcodeGenerator1.verifyResponseCode(123456789123456789L, response1Long)); assertFalse(passcodeGenerator1.verifyResponseCode(123456789123456789L, "boguscode")); // test with (long, null), response code should be same as with just long String response1LongNull = passcodeGenerator1.generateResponseCode(123456789123456789L, null); assertEquals(response1Long, response1LongNull); // test with byte[] using base32 encoded representation of byte array created from 0L String response1ByteArray = passcodeGenerator1.generateResponseCode(Base32String.decode("AG3JWS5M2BPRK")); assertEquals(response1Long, response1ByteArray); // test Long with another key bytes. String response2Long = passcodeGenerator2.generateResponseCode(123456789123456789L); assertTrue(passcodeGenerator2.verifyResponseCode(123456789123456789L, response2Long)); }