Skip to content
This repository has been archived by the owner on Feb 16, 2022. It is now read-only.

Commit

Permalink
SecretService is no longer an interface
Browse files Browse the repository at this point in the history
  • Loading branch information
sawano committed Apr 14, 2017
1 parent 917d6cc commit d9b4017
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 71 deletions.
43 changes: 41 additions & 2 deletions src/main/java/se/sawano/java/security/otp/SecretService.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,34 @@

package se.sawano.java.security.otp;

public interface SecretService {
import se.sawano.java.security.otp.ShaAlgorithm;
import se.sawano.java.security.otp.SharedSecret;
import se.sawano.java.security.otp.impl.DefaultRandomSupplier;
import se.sawano.java.security.otp.impl.RandomSupplier;

import java.util.HashMap;
import java.util.Map;

import static org.apache.commons.lang3.Validate.notNull;

public class SecretService {

private final RandomSupplier random;
private final Map<ShaAlgorithm, Integer> algorithmToNumberOfBytes = new HashMap<>();

public SecretService() {
this(new DefaultRandomSupplier());
}

public SecretService(final RandomSupplier random) {
notNull(random);

this.random = random;

algorithmToNumberOfBytes.put(ShaAlgorithm.SHA1, 20);
algorithmToNumberOfBytes.put(ShaAlgorithm.SHA256, 32);
algorithmToNumberOfBytes.put(ShaAlgorithm.SHA512, 64);
}

/**
* Create a new shared secret.
Expand All @@ -26,6 +53,18 @@ public interface SecretService {
*
* @return the newly generated secret
*/
SharedSecret generateSharedSecret(ShaAlgorithm algorithm);
public SharedSecret generateSharedSecret(final ShaAlgorithm algorithm) {
notNull(algorithm);

return generateSharedSecret(algorithm, algorithmToNumberOfBytes.get(algorithm));
}

public SharedSecret generateSharedSecret(final ShaAlgorithm algorithm, final int numberOfBytes) {
final byte[] bytes = new byte[numberOfBytes];

random.nextBytes(bytes);

return SharedSecret.from(bytes, algorithm);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import se.sawano.java.security.otp.*;
import se.sawano.java.security.otp.TOTPService;
import se.sawano.java.security.otp.SecretService;
import se.sawano.java.security.otp.user.UserId;
import se.sawano.java.security.otp.user.persistence.SecretRepository;
import se.sawano.java.security.otp.user.persistence.TOTPRegistry;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@
* limitations under the License.
*/

package se.sawano.java.security.otp.impl;
package se.sawano.java.security.otp;

import org.junit.Test;
import se.sawano.java.security.otp.SecretService;
import se.sawano.java.security.otp.ShaAlgorithm;
import se.sawano.java.security.otp.SharedSecret;
import se.sawano.java.security.otp.impl.RandomSupplier;

import static org.junit.Assert.assertEquals;
import static se.sawano.java.security.otp.ShaAlgorithm.*;

public class DefaultSecretServiceTests {
public class SecretServiceTests {

public static final String ASCII_SECRET_FROM_RFC6238 = "12345678901234567890";
public static final String EXPECTED_SHA1_HEX_SECRET_FROM_RFC_6238_EXAMPLE = "3132333435363738393031323334353637383930";
Expand Down Expand Up @@ -73,7 +75,7 @@ private void givenAlgorithm(final ShaAlgorithm algorithm) {
}

private void whenGeneratingSecret() {
final DefaultSecretService secretService = new DefaultSecretService(fakeRandomSupplier());
final SecretService secretService = new SecretService(fakeRandomSupplier());
secret = secretService.generateSharedSecret(algorithm);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@

import org.apache.commons.codec.binary.Base32;
import org.junit.Test;
import se.sawano.java.security.otp.SharedSecret;
import se.sawano.java.security.otp.TOTP;
import se.sawano.java.security.otp.TOTPService;
import se.sawano.java.security.otp.impl.DefaultSecretService;

import static se.sawano.java.security.otp.ShaAlgorithm.SHA1;
import static se.sawano.java.security.otp.ShaAlgorithm.SHA256;
Expand All @@ -34,7 +30,7 @@ public class TestDataHelper {

@Test
public void should_generate_new_secret() throws Exception {
final SharedSecret secret = new DefaultSecretService().generateSharedSecret(SHA1);
final SharedSecret secret = new SecretService().generateSharedSecret(SHA1);

System.out.println(new Base32(false).encodeAsString(secret.value()));

Expand Down

0 comments on commit d9b4017

Please sign in to comment.