Replace bouncycastle Hex with guava Hex

Hex encoding and decoding compatibility has been verified regarding
null, "", character case handling, and incorrect digits handling.
This commit is contained in:
battleofwizards 2019-09-03 09:24:03 +02:00
parent 9047ff17a2
commit 7719851658
No known key found for this signature in database
GPG Key ID: 58B1485148D203E1
5 changed files with 40 additions and 11 deletions

View File

@ -18,8 +18,7 @@
package bisq.common.crypto;
import bisq.common.util.Utilities;
import org.bouncycastle.util.encoders.Hex;
import bisq.common.util.Hex;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
@ -177,7 +176,7 @@ public class Encryption {
public static byte[] decryptPayloadWithHmac(byte[] encryptedPayloadWithHmac, SecretKey secretKey) throws CryptoException {
byte[] payloadWithHmac = decrypt(encryptedPayloadWithHmac, secretKey);
String payloadWithHmacAsHex = Hex.toHexString(payloadWithHmac);
String payloadWithHmacAsHex = Hex.encode(payloadWithHmac);
// first part is raw message
int length = payloadWithHmacAsHex.length();
int sep = length - 64;

View File

@ -17,6 +17,8 @@
package bisq.common.crypto;
import bisq.common.util.Hex;
import com.google.common.base.Charsets;
import org.bouncycastle.bcpg.BCPGKey;
@ -27,7 +29,6 @@ import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPUtil;
import org.bouncycastle.openpgp.jcajce.JcaPGPPublicKeyRingCollection;
import org.bouncycastle.util.encoders.Hex;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
@ -71,7 +72,7 @@ public class PGP {
log.debug(pgpPublicKey.getClass().getName()
+ " KeyID: " + Long.toHexString(pgpPublicKey.getKeyID())
+ " type: " + pgpPublicKey.getAlgorithm()
+ " fingerprint: " + new String(Hex.encode(pgpPublicKey.getFingerprint())));
+ " fingerprint: " + Hex.encode(pgpPublicKey.getFingerprint()));
BCPGKey bcKey = pgpPublicKey.getPublicKeyPacket().getKey();
log.debug(bcKey.getClass().getName());

View File

@ -0,0 +1,31 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/
package bisq.common.util;
import com.google.common.io.BaseEncoding;
public class Hex {
public static byte[] decode(String hex) {
return BaseEncoding.base16().lowerCase().decode(hex.toLowerCase());
}
public static String encode(byte[] bytes) {
return BaseEncoding.base16().lowerCase().encode(bytes);
}
}

View File

@ -60,6 +60,7 @@ import bisq.common.proto.persistable.PersistedDataHost;
import bisq.common.storage.Storage;
import bisq.common.util.Tuple2;
import bisq.common.util.Utilities;
import bisq.common.util.Hex;
import com.google.protobuf.ByteString;
@ -69,8 +70,6 @@ import com.google.common.annotations.VisibleForTesting;
import org.apache.commons.lang3.StringUtils;
import org.bouncycastle.util.encoders.Hex;
import java.security.KeyPair;
import java.security.PublicKey;
@ -890,7 +889,7 @@ public class P2PDataStorage implements MessageListener, ConnectionListener, Pers
@Override
public String toString() {
return "ByteArray{" +
"bytes as Hex=" + Hex.toHexString(bytes) +
"bytes as Hex=" + Hex.encode(bytes) +
'}';
}

View File

@ -19,6 +19,7 @@ package bisq.price.spot.providers;
import bisq.price.spot.ExchangeRate;
import bisq.price.spot.ExchangeRateProvider;
import bisq.common.util.Hex;
import org.knowm.xchange.bitcoinaverage.dto.marketdata.BitcoinAverageTicker;
import org.knowm.xchange.bitcoinaverage.dto.marketdata.BitcoinAverageTickers;
@ -32,8 +33,6 @@ import org.springframework.web.util.UriComponentsBuilder;
import com.google.common.base.Charsets;
import org.bouncycastle.util.encoders.Hex;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
@ -120,7 +119,7 @@ public abstract class BitcoinAverage extends ExchangeRateProvider {
protected String getAuthSignature() {
String payload = String.format("%s.%s", Instant.now().getEpochSecond(), pubKey);
return String.format("%s.%s", payload, Hex.toHexString(mac.doFinal(payload.getBytes(Charsets.UTF_8))));
return String.format("%s.%s", payload, Hex.encode(mac.doFinal(payload.getBytes(Charsets.UTF_8))));
}
private static Mac initMac(String privKey) {