Skip to content

Commit

Permalink
Fix #554: Remove Guava dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
banterCZ committed Dec 19, 2023
1 parent a7aa2f5 commit 5a14440
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 41 deletions.
7 changes: 0 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,12 @@
<maven-javadoc-plugin.version>3.6.3</maven-javadoc-plugin.version>
<maven-source-plugin.version>3.3.0</maven-source-plugin.version>
<maven-surefire-plugin.version>3.2.3</maven-surefire-plugin.version>
<guava.version>32.1.3-jre</guava.version>
<slf4j.version>2.0.9</slf4j.version>
<junit.version>5.10.1</junit.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down
4 changes: 0 additions & 4 deletions powerauth-java-crypto/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@
</parent>

<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
*/
package io.getlime.security.powerauth.crypto.lib.generator;

import com.google.common.io.BaseEncoding;
import io.getlime.security.powerauth.crypto.lib.encryptor.ecies.kdf.KdfX9_63;
import io.getlime.security.powerauth.crypto.lib.model.RecoveryInfo;
import io.getlime.security.powerauth.crypto.lib.model.RecoverySeed;
import io.getlime.security.powerauth.crypto.lib.model.exception.CryptoProviderException;
import io.getlime.security.powerauth.crypto.lib.model.exception.GenericCryptoException;
import io.getlime.security.powerauth.crypto.lib.util.CRC16;
import io.getlime.security.powerauth.crypto.lib.util.KeyConvertor;
import org.bouncycastle.util.encoders.Base32;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -67,6 +67,8 @@ public class IdentifierGenerator {
*/
private static final int PUK_DERIVATION_MAX_ATTEMPTS = 20;

private static final String PADDING = "====";

private final KeyGenerator keyGenerator = new KeyGenerator();
private final KeyConvertor keyConvertor = new KeyConvertor();

Expand All @@ -79,17 +81,6 @@ public String generateActivationId() {
return UUID.randomUUID().toString();
}

/**
* Generate a new string of a default length (5) with characters from Base32 encoding.
*
* @return New string with Base32 characters of a given length.
* @throws CryptoProviderException In case key cryptography provider is incorrectly initialized.
*/
private String generateBase32Token() throws CryptoProviderException {
byte[] randomBytes = keyGenerator.generateRandomBytes(BASE32_KEY_LENGTH);
return BaseEncoding.base32().omitPadding().encode(randomBytes).substring(0, BASE32_KEY_LENGTH);
}

/**
* Generate version 3.0 or higher activation code. The format of activation code is "ABCDE-FGHIJ-KLMNO-PQRST".
* <p>
Expand Down Expand Up @@ -171,7 +162,7 @@ public boolean validateActivationCode(String activationCode) {
}

// Decode the Base32 value
byte[] activationCodeBytes = BaseEncoding.base32().decode(activationCode.replace("-", ""));
byte[] activationCodeBytes = Base32.decode(activationCode.replace("-", "") + PADDING);

// Verify byte array length
if (activationCodeBytes.length != ACTIVATION_CODE_BYTES_LENGTH) {
Expand Down Expand Up @@ -367,9 +358,9 @@ private String generatePuk(SecretKey recoveryPukBaseKey, byte[] indexBytes) thro
* @param activationCodeBytes Raw activation code bytes.
* @return Base32 String representation of activation code.
*/
private String encodeActivationCode(byte[] activationCodeBytes) {
// Generate Base32 representation from 12 activation code bytes, without padding characters.
String base32Encoded = BaseEncoding.base32().omitPadding().encode(activationCodeBytes);
private String encodeActivationCode(final byte[] activationCodeBytes) {
// Generate Base32 representation from 12 activation code bytes
final String base32Encoded = Base32.toBase32String(activationCodeBytes);

// Split Base32 string into 4 groups, each one contains 5 characters. Use "-" as separator.
return base32Encoded.substring(0, BASE32_KEY_LENGTH)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
package io.getlime.security.powerauth.crypto.lib.totp;

import com.google.common.base.Strings;
import io.getlime.security.powerauth.crypto.lib.model.exception.CryptoProviderException;
import org.bouncycastle.util.Arrays;
import org.slf4j.Logger;
Expand Down Expand Up @@ -268,7 +267,7 @@ private static long countTimeSteps(final Instant instant, final Duration stepLen
}

private static String padWithZeros(final String source, final int length) {
return Strings.padStart(source, length, '0');
return String.format("%1$" + length + "s", source).replace(' ', '0');
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
package io.getlime.security.powerauth.crypto.lib.util;

import com.google.common.base.Joiner;
import io.getlime.security.powerauth.crypto.lib.config.DecimalSignatureConfiguration;
import io.getlime.security.powerauth.crypto.lib.config.PowerAuthConfiguration;
import io.getlime.security.powerauth.crypto.lib.config.SignatureConfiguration;
Expand Down Expand Up @@ -164,7 +163,7 @@ private String computePowerAuthDecimalSignature(byte[] data, List<SecretKey> sig
signatureStringComponents[i] = String.format("%0" + signatureDecimalLength + "d", number);
}
// Join components with dash.
return Joiner.on("-").join(signatureStringComponents);
return String.join("-", signatureStringComponents);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,25 @@
import io.getlime.security.powerauth.crypto.lib.generator.KeyGenerator;
import io.getlime.security.powerauth.crypto.lib.model.RecoveryInfo;
import io.getlime.security.powerauth.crypto.lib.model.RecoverySeed;
import io.getlime.security.powerauth.crypto.lib.model.exception.CryptoProviderException;
import io.getlime.security.powerauth.crypto.lib.model.exception.GenericCryptoException;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import javax.crypto.SecretKey;
import java.security.*;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Security;
import java.util.HashSet;

import static org.junit.jupiter.api.Assertions.*;

/**
* Test for {@link IdentifierGenerator}.
*
* @author Roman Strobl, [email protected]
*/
public class PowerAuthRecoveryCodeTest {
class IdentifierGeneratorTest {

private final IdentifierGenerator identifierGenerator = new IdentifierGenerator();

Expand All @@ -50,7 +52,7 @@ public static void setUp() {
}

@Test
public void testRecoveryCodeDerivation() throws CryptoProviderException, InvalidKeyException, GenericCryptoException {
void testRecoveryCodeDerivation() throws Exception {
// Number of PUKs to test
int pukCount = 100;

Expand Down Expand Up @@ -89,4 +91,14 @@ public void testRecoveryCodeDerivation() throws CryptoProviderException, Invalid
}
}

@Test
void testGenerateActivationCode() throws Exception {
final IdentifierGenerator tested = new IdentifierGenerator();

final String result = tested.generateActivationCode(new byte[10]);

// Base32 is AAAAAAAAAAAAAAAAAAAA====
assertEquals("AAAAA-AAAAA-AAAAA-AAAAA", result);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
* Test for {@link Totp}.
*
* @author Lubos Racansky, [email protected]
*
*/
class TotpTest {

Expand Down
4 changes: 0 additions & 4 deletions powerauth-java-http/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@
<artifactId>powerauth-java-crypto</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down

0 comments on commit 5a14440

Please sign in to comment.