Skip to content

Commit

Permalink
Refactor the code
Browse files Browse the repository at this point in the history
  • Loading branch information
Bhashinee committed Nov 28, 2023
1 parent 0a1b654 commit b854d9f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
12 changes: 6 additions & 6 deletions ballerina/tests/sign_verify_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,9 @@ isolated function testDecodeRsaPrivateKeyError() returns Error? {
};
PrivateKey|Error privateKey = decodeRsaPrivateKeyFromKeyStore(keyStore, "ec-keypair", "ballerina");
if privateKey is Error {
test:assertEquals(privateKey.message(), "Not a valid RSA key.");
test:assertEquals(privateKey.message(), "Not a valid RSA key");
} else {
test:assertFail("Expected error not found.");
test:assertFail("Expected error not found");
}
}

Expand All @@ -266,9 +266,9 @@ isolated function testDecodeEcPrivateKeyError() returns Error? {
};
PrivateKey|Error privateKey = decodeEcPrivateKeyFromKeyStore(keyStore, "ballerina", "ballerina");
if privateKey is Error {
test:assertEquals(privateKey.message(), "Not a valid EC key.");
test:assertEquals(privateKey.message(), "Not a valid EC key");
} else {
test:assertFail("Expected error not found.");
test:assertFail("Expected error not found");
}
}

Expand All @@ -280,7 +280,7 @@ isolated function testDecodeEcPublicKeyError() returns Error? {
};
PublicKey|Error publicKey = decodeEcPublicKeyFromTrustStore(keyStore, "ballerina");
if publicKey is Error {
test:assertEquals(publicKey.message(), "Not a valid EC public key.");
test:assertEquals(publicKey.message(), "Not a valid EC public key");
} else {
test:assertFail("Expected error not found");
}
Expand All @@ -294,7 +294,7 @@ isolated function testDecodeRsaPublicKeyError() returns Error? {
};
PublicKey|Error publicKey = decodeRsaPublicKeyFromTrustStore(keyStore, "ec-keypair");
if publicKey is Error {
test:assertEquals(publicKey.message(), "Not a valid RSA public key.");
test:assertEquals(publicKey.message(), "Not a valid RSA public key");
} else {
test:assertFail("Expected error not found");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,8 @@ public static Object decodeRsaPrivateKeyFromKeyStore(BMap<BString, BString> keyS
if (decodedPrivateKey instanceof PrivateKey) {
PrivateKey privateKey = (PrivateKey) decodedPrivateKey;
return buildRsPrivateKeyRecord(privateKey);
} else {
return decodedPrivateKey;
}
return decodedPrivateKey;
}

public static Object decodeEcPrivateKeyFromKeyStore(BMap<BString, BString> keyStoreRecord, BString keyAlias,
Expand All @@ -90,9 +89,8 @@ public static Object decodeEcPrivateKeyFromKeyStore(BMap<BString, BString> keySt
if (decodedPrivateKey instanceof PrivateKey) {
PrivateKey privateKey = (PrivateKey) decodedPrivateKey;
return buildEcPrivateKeyRecord(privateKey);
} else {
return decodedPrivateKey;
}
return decodedPrivateKey;

Check warning on line 93 in native/src/main/java/io/ballerina/stdlib/crypto/nativeimpl/Decode.java

View check run for this annotation

Codecov / codecov/patch

native/src/main/java/io/ballerina/stdlib/crypto/nativeimpl/Decode.java#L93

Added line #L93 was not covered by tests
}

private static Object getPrivateKey(BMap<BString, BString> keyStoreRecord, BString keyAlias, BString keyPassword) {
Expand Down Expand Up @@ -168,7 +166,7 @@ private static Object buildRsPrivateKeyRecord(PrivateKey privateKey) {
if (privateKey.getAlgorithm().equals(Constants.RSA_ALGORITHM)) {
return getPrivateKeyRecord(privateKey);
} else {
return CryptoUtils.createError("Not a valid RSA key.");
return CryptoUtils.createError("Not a valid RSA key");
}
}

Expand All @@ -185,7 +183,7 @@ private static Object buildEcPrivateKeyRecord(PrivateKey privateKey) {
if (privateKey.getAlgorithm().equals(Constants.EC_ALGORITHM)) {
return getPrivateKeyRecord(privateKey);
} else {
return CryptoUtils.createError("Not a valid EC key.");
return CryptoUtils.createError("Not a valid EC key");
}
}

Expand Down Expand Up @@ -247,7 +245,7 @@ private static Object buildRsaPublicKeyRecord(Certificate certificate) {
if (publicKey.getAlgorithm().equals(Constants.RSA_ALGORITHM)) {
return getPublicKeyRecord(certificate, certificateBMap, publicKey);
}
return CryptoUtils.createError("Not a valid RSA public key.");
return CryptoUtils.createError("Not a valid RSA public key");
}

private static Object buildEcPublicKeyRecord(Certificate certificate) {
Expand All @@ -256,7 +254,7 @@ private static Object buildEcPublicKeyRecord(Certificate certificate) {
if (publicKey.getAlgorithm().equals(Constants.EC_ALGORITHM)) {
return getPublicKeyRecord(certificate, certificateBMap, publicKey);
}
return CryptoUtils.createError("Not a valid EC public key.");
return CryptoUtils.createError("Not a valid EC public key");
}

private static Object getPublicKeyRecord(Certificate certificate, BMap<BString, Object> certificateBMap,
Expand Down

0 comments on commit b854d9f

Please sign in to comment.