8210838: Override javax.crypto.Cipher.toString()

Reviewed-by: mullan, weijun
This commit is contained in:
Sean Coffey 2018-11-19 09:56:42 +00:00
parent dc4be9066f
commit 63b433923c
2 changed files with 66 additions and 39 deletions

View file

@ -1183,21 +1183,6 @@ public class Cipher {
}
}
private static String getOpmodeString(int opmode) {
switch (opmode) {
case ENCRYPT_MODE:
return "encryption";
case DECRYPT_MODE:
return "decryption";
case WRAP_MODE:
return "key wrapping";
case UNWRAP_MODE:
return "key unwrapping";
default:
return "";
}
}
/**
* Initializes this cipher with a key.
*
@ -1325,9 +1310,7 @@ public class Cipher {
this.opmode = opmode;
if (!skipDebug && pdebug != null) {
pdebug.println("Cipher." + transformation + " " +
getOpmodeString(opmode) + " algorithm from: " +
getProviderName());
pdebug.println(this.toString());
}
}
@ -1468,9 +1451,7 @@ public class Cipher {
this.opmode = opmode;
if (!skipDebug && pdebug != null) {
pdebug.println("Cipher." + transformation + " " +
getOpmodeString(opmode) + " algorithm from: " +
getProviderName());
pdebug.println(this.toString());
}
}
@ -1611,9 +1592,7 @@ public class Cipher {
this.opmode = opmode;
if (!skipDebug && pdebug != null) {
pdebug.println("Cipher." + transformation + " " +
getOpmodeString(opmode) + " algorithm from: " +
getProviderName());
pdebug.println(this.toString());
}
}
@ -1688,8 +1667,7 @@ public class Cipher {
/**
* Initializes this cipher with the public key from the given certificate
* and
* a source of randomness.
* and a source of randomness.
*
* <p>The cipher is initialized for one of the following four operations:
* encryption, decryption, key wrapping
@ -1757,8 +1735,7 @@ public class Cipher {
initialized = false;
checkOpmode(opmode);
// Check key usage if the certificate is of
// type X.509.
// Check key usage if the certificate is of type X.509.
if (certificate instanceof java.security.cert.X509Certificate) {
// Check whether the cert has a key usage extension
// marked as a critical extension.
@ -1801,9 +1778,7 @@ public class Cipher {
this.opmode = opmode;
if (!skipDebug && pdebug != null) {
pdebug.println("Cipher." + transformation + " " +
getOpmodeString(opmode) + " algorithm from: " +
getProviderName());
pdebug.println(this.toString());
}
}
@ -2825,4 +2800,44 @@ public class Cipher {
}
spi.engineUpdateAAD(src);
}
}
/**
* Returns a String representation of this Cipher.
*
* @implNote
* This implementation returns a String containing the transformation,
* mode, and provider of this Cipher.
* The exact format of the String is unspecified and is subject to change.
*
* @return a String describing this Cipher
*/
@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append("Cipher.")
.append(transformation)
.append(", mode: ");
switch (opmode) {
case 0:
sb.append("not initialized");
break;
case ENCRYPT_MODE:
sb.append("encryption");
break;
case DECRYPT_MODE:
sb.append("decryption");
break;
case WRAP_MODE:
sb.append("key wrapping");
break;
case UNWRAP_MODE:
sb.append("key unwrapping");
break;
default:
// should never happen
sb.append("error:").append(Integer.toString(opmode));
}
sb.append(", algorithm from: ").append(getProviderName());
return sb.toString();
}
}