Skip to content

Commit

Permalink
Use data type converter to convert base64. (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
OGKevin committed May 5, 2018
1 parent eec7bf1 commit 263ae6e
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions src/main/java/com/bunq/sdk/security/SecurityUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,9 @@ public static String getPrivateKeyFormattedString(KeyPair keyPair) {
*/
private static String getPrivateKeyFormattedString(PrivateKey privateKey) {
byte[] encodedPrivateKey = privateKey.getEncoded();
byte[] base64 = Base64.getEncoder().encode(encodedPrivateKey);
String privateKeyString = DatatypeConverter.printBase64Binary(encodedPrivateKey);

return String.format(PRIVATE_KEY_FORMAT, new String(base64));
return String.format(PRIVATE_KEY_FORMAT, privateKeyString);
}

/**
Expand Down Expand Up @@ -230,7 +230,7 @@ private static byte[] getAllPublicKeyByteFromFormattedString(String publicKeyStr
publicKeyString = publicKeyString.replace(PUBLIC_KEY_END, STRING_EMPTY);
publicKeyString = publicKeyString.replace(NEWLINE, STRING_EMPTY);

return Base64.getDecoder().decode(publicKeyString);
return DatatypeConverter.parseBase64Binary(publicKeyString);
}

/**
Expand All @@ -257,7 +257,7 @@ private static byte[] getAllPrivateKeyByteFromFormattedString(String privateKeyS
privateKeyString = privateKeyString.replace(PRIVATE_KEY_END, STRING_EMPTY);
privateKeyString = privateKeyString.replace(NEWLINE, STRING_EMPTY);

return Base64.getDecoder().decode(privateKeyString);
return DatatypeConverter.parseBase64Binary(privateKeyString);
}

public static String getPublicKeyFormattedString(KeyPair keyPair) {
Expand All @@ -269,9 +269,9 @@ public static String getPublicKeyFormattedString(KeyPair keyPair) {
*/
public static String getPublicKeyFormattedString(PublicKey publicKey) {
byte[] encodedPublicKey = publicKey.getEncoded();
byte[] base64 = Base64.getEncoder().encode(encodedPublicKey);
String publicKeyString = DatatypeConverter.printBase64Binary(encodedPublicKey);

return String.format(PUBLIC_KEY_FORMAT, new String(base64));
return String.format(PUBLIC_KEY_FORMAT, publicKeyString);
}

/**
Expand Down Expand Up @@ -308,22 +308,27 @@ private static byte[] generateInitializationVector() {
return initializationVector;
}

private static void addHeaderClientEncryptionKey(ApiContext apiContext, SecretKey key,
Map<String, String> customHeaders) {
private static void addHeaderClientEncryptionKey(
ApiContext apiContext,
SecretKey key,
Map<String, String> customHeaders
) {
try {
Cipher cipher = Cipher.getInstance(KEY_CIPHER_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, apiContext.getInstallationContext().getPublicKeyServer());
byte[] keyEncrypted = cipher.doFinal(key.getEncoded());
String keyEncryptedEncoded = Base64.getEncoder().encodeToString(keyEncrypted);
String keyEncryptedEncoded = DatatypeConverter.printBase64Binary(keyEncrypted);
customHeaders.put(HEADER_CLIENT_ENCRYPTION_KEY, keyEncryptedEncoded);
} catch (GeneralSecurityException exception) {
throw new BunqException(exception.getMessage());
}
}

private static void addHeaderClientEncryptionIv(byte[] initializationVector, Map<String,
String> customHeaders) {
String initializationVectorEncoded = Base64.getEncoder().encodeToString(initializationVector);
private static void addHeaderClientEncryptionIv(
byte[] initializationVector,
Map<String, String> customHeaders
) {
String initializationVectorEncoded = DatatypeConverter.printBase64Binary(initializationVector);

customHeaders.put(HEADER_CLIENT_ENCRYPTION_IV, initializationVectorEncoded);
}
Expand Down Expand Up @@ -357,7 +362,7 @@ private static void addHeaderClientEncryptionHmac(byte[] requestBytes,
bufferedSink.flush();
bufferedSink.close();
byte[] hmac = mac.doFinal();
String hmacEncoded = Base64.getEncoder().encodeToString(hmac);
String hmacEncoded = DatatypeConverter.printBase64Binary(hmac);
customHeaders.put(HEADER_CLIENT_ENCRYPTION_HMAC, hmacEncoded);
} catch (GeneralSecurityException | IOException exception) {
throw new BunqException(exception.getMessage());
Expand Down Expand Up @@ -439,7 +444,7 @@ private static String signBase64(byte[] bytesToSign, KeyPair keyPair) throws Bun
byte[] dataBytesSigned = signDataWithSignature(signature, bytesToSign);
verifyDataSigned(signature, keyPair.getPublic(), bytesToSign, dataBytesSigned);

return Base64.getEncoder().encodeToString(dataBytesSigned);
return DatatypeConverter.printBase64Binary(dataBytesSigned);
}

private static Signature getSignatureInstance() throws BunqException {
Expand Down Expand Up @@ -501,7 +506,7 @@ public static void validateResponseSignature(
response.header(HEADER_SERVER_SIGNATURE)
);
byte[] serverSignatureBase64Bytes = headerServerSignature.getValue().getBytes();
byte[] serverSignatureDecoded = Base64.getDecoder().decode(serverSignatureBase64Bytes);
byte[] serverSignatureDecoded = DatatypeConverter.printBase64Binary(serverSignatureBase64Bytes).getBytes();
verifyDataSigned(signature, keyPublicServer, responseBytes, serverSignatureDecoded);
}

Expand Down

0 comments on commit 263ae6e

Please sign in to comment.