This repository has been archived by the owner on Feb 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
133 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
src/test/java/se/sawano/java/security/otp/impl/DefaultSecretServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* Copyright 2017 Daniel Sawano | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package se.sawano.java.security.otp.impl; | ||
|
||
import org.junit.Test; | ||
import se.sawano.java.security.otp.ShaAlgorithm; | ||
import se.sawano.java.security.otp.SharedSecret; | ||
import se.sawano.java.security.otp.TOTP; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static se.sawano.java.security.otp.ShaAlgorithm.*; | ||
|
||
public class DefaultSecretServiceTest { | ||
|
||
public static final String ASCII_SECRET_FROM_RFC6238 = "12345678901234567890"; | ||
public static final String EXPECTED_SHA1_HEX_SECRET_FROM_RFC_6238_EXAMPLE = "3132333435363738393031323334353637383930"; | ||
public static final String EXPECTED_SHA256_HEX_SECRET_FROM_RFC_6238_EXAMPLE = "3132333435363738393031323334353637383930313233343536373839303132"; | ||
public static final String EXPECTED_SHA512_HEX_SECRET_FROM_RFC_6238_EXAMPLE = "3132333435363738393031323334353637383930" + | ||
"3132333435363738393031323334353637383930" + | ||
"3132333435363738393031323334353637383930" + | ||
"31323334"; | ||
private ShaAlgorithm algorithm; | ||
private SharedSecret secret; | ||
|
||
@Test | ||
public void should_generate_sha1_secret() throws Exception { | ||
givenAlgorithm(SHA1); | ||
|
||
whenGeneratingSecret(); | ||
|
||
thenSecretShouldHaveCorrectAlgorithm(); | ||
thenNumberOfBytesInSecretIs(20); | ||
thenHexSecretIs(EXPECTED_SHA1_HEX_SECRET_FROM_RFC_6238_EXAMPLE); | ||
} | ||
|
||
@Test | ||
public void should_generate_sha256_secret() throws Exception { | ||
givenAlgorithm(SHA256); | ||
|
||
whenGeneratingSecret(); | ||
|
||
thenSecretShouldHaveCorrectAlgorithm(); | ||
thenNumberOfBytesInSecretIs(32); | ||
thenHexSecretIs(EXPECTED_SHA256_HEX_SECRET_FROM_RFC_6238_EXAMPLE); | ||
} | ||
|
||
@Test | ||
public void should_generate_sha512_secret() throws Exception { | ||
givenAlgorithm(SHA512); | ||
|
||
whenGeneratingSecret(); | ||
|
||
thenSecretShouldHaveCorrectAlgorithm(); | ||
thenNumberOfBytesInSecretIs(64); | ||
thenHexSecretIs(EXPECTED_SHA512_HEX_SECRET_FROM_RFC_6238_EXAMPLE); | ||
} | ||
|
||
@Test | ||
public void should_print_totp() throws Exception { | ||
final String secretB32 = "ZEJHUB2WISYTMOUMDNM7GO5URLKS7TXC"; | ||
|
||
final SharedSecret secret = SharedSecret.fromBase32(secretB32, SHA1); | ||
|
||
final TOTP totp = new DefaultTOTPService().create(secret, TOTP.Length.SIX, secret.algorithm()); | ||
|
||
System.out.println(totp.value()); | ||
} | ||
|
||
private void givenAlgorithm(final ShaAlgorithm algorithm) { | ||
this.algorithm = algorithm; | ||
} | ||
|
||
private void whenGeneratingSecret() { | ||
final String s = (ASCII_SECRET_FROM_RFC6238 + ASCII_SECRET_FROM_RFC6238 + ASCII_SECRET_FROM_RFC6238 + ASCII_SECRET_FROM_RFC6238); | ||
final RandomSupplier randomSupplier = bytes -> System.arraycopy(s.getBytes(), 0, bytes, 0, bytes.length); | ||
final DefaultSecretService secretService = new DefaultSecretService(randomSupplier); | ||
secret = secretService.generateSharedSecret(algorithm); | ||
} | ||
|
||
private void thenSecretShouldHaveCorrectAlgorithm() { | ||
assertEquals(algorithm, secret.algorithm()); | ||
} | ||
|
||
private void thenNumberOfBytesInSecretIs(final int expectedNumberOfBytes) { | ||
assertEquals(expectedNumberOfBytes, secret.value().length); | ||
} | ||
|
||
private void thenHexSecretIs(final String expectedHexSecret) { | ||
assertEquals(expectedHexSecret, secret.asHexString()); | ||
} | ||
} |