Repository: commons-crypto Updated Branches: refs/heads/master 33be50bd9 -> 6ea9d3d89
Tidy example docs Project: http://git-wip-us.apache.org/repos/asf/commons-crypto/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-crypto/commit/6ea9d3d8 Tree: http://git-wip-us.apache.org/repos/asf/commons-crypto/tree/6ea9d3d8 Diff: http://git-wip-us.apache.org/repos/asf/commons-crypto/diff/6ea9d3d8 Branch: refs/heads/master Commit: 6ea9d3d89d7ea262534c2f1c15fc18e2953a505e Parents: 33be50b Author: Sebb <[email protected]> Authored: Tue Jun 28 01:33:50 2016 +0100 Committer: Sebb <[email protected]> Committed: Tue Jun 28 01:33:50 2016 +0100 ---------------------------------------------------------------------- .../crypto/examples/CipherByteArrayExample.java | 31 +++++------ .../examples/CipherByteBufferExample.java | 54 +++++++++----------- .../commons/crypto/examples/RandomExample.java | 7 --- .../commons/crypto/examples/StreamExample.java | 27 ++++------ 4 files changed, 49 insertions(+), 70 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/6ea9d3d8/src/main/java/org/apache/commons/crypto/examples/CipherByteArrayExample.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/crypto/examples/CipherByteArrayExample.java b/src/main/java/org/apache/commons/crypto/examples/CipherByteArrayExample.java index a065755..f36ab58 100644 --- a/src/main/java/org/apache/commons/crypto/examples/CipherByteArrayExample.java +++ b/src/main/java/org/apache/commons/crypto/examples/CipherByteArrayExample.java @@ -33,29 +33,16 @@ import org.apache.commons.crypto.utils.Utils; */ public class CipherByteArrayExample { - /** - * Converts String to UTF8 bytes - * - * @param input the input string - * @return UTF8 bytes - */ - private static byte[] getUTF8Bytes(String input) { - return input.getBytes(StandardCharsets.UTF_8); - } - - /** - * Main method - * - * @param args args of main - * @throws Exception when encryption/decryption failed - */ public static void main(String[] args) throws Exception { + final SecretKeySpec key = new SecretKeySpec(getUTF8Bytes("1234567890123456"),"AES"); final IvParameterSpec iv = new IvParameterSpec(getUTF8Bytes("1234567890123456")); + Properties properties = new Properties(); //Creates a CryptoCipher instance with the transformation and properties. final String transform = "AES/CBC/PKCS5Padding"; CryptoCipher encipher = Utils.getCipherInstance(transform, properties); + System.out.println("Cipher: " + encipher.getClass().getCanonicalName()); final String sampleInput = "hello world!"; System.out.println("input: " + sampleInput); @@ -68,7 +55,7 @@ public class CipherByteArrayExample { //Continues a multiple-part encryption/decryption operation for byte array. int updateBytes = encipher.update(input, 0, input.length, output, 0); System.out.println(updateBytes); - //We should call do final at the end of encryption/decryption. + //We must call doFinal at the end of encryption/decryption. int finalBytes = encipher.doFinal(input, 0, 0, output, updateBytes); System.out.println(finalBytes); //Closes the cipher. @@ -85,4 +72,14 @@ public class CipherByteArrayExample { System.out.println("output: " + new String(decoded, StandardCharsets.UTF_8)); } + /** + * Converts String to UTF8 bytes + * + * @param input the input string + * @return UTF8 bytes + */ + private static byte[] getUTF8Bytes(String input) { + return input.getBytes(StandardCharsets.UTF_8); + } + } http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/6ea9d3d8/src/main/java/org/apache/commons/crypto/examples/CipherByteBufferExample.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/crypto/examples/CipherByteBufferExample.java b/src/main/java/org/apache/commons/crypto/examples/CipherByteBufferExample.java index 8950e66..c515461 100644 --- a/src/main/java/org/apache/commons/crypto/examples/CipherByteBufferExample.java +++ b/src/main/java/org/apache/commons/crypto/examples/CipherByteBufferExample.java @@ -30,39 +30,10 @@ import org.apache.commons.crypto.cipher.CryptoCipher; import org.apache.commons.crypto.utils.Utils; /** - * Example showing use of the CryptoCipher API using a ByteBuffer + * Example showing the CryptoCipher API using a ByteBuffer */ public class CipherByteBufferExample { - /** - * Converts String to UTF8 bytes - * - * @param input the input string - * @return UTF8 bytes - */ - private static byte[] getUTF8Bytes(String input) { - return input.getBytes(StandardCharsets.UTF_8); - } - - /** - * Converts ByteBuffer to String - * - * @param buffer input byte buffer - * @return the converted string - */ - private static String asString(ByteBuffer buffer) { - final ByteBuffer copy = buffer.duplicate(); - final byte[] bytes = new byte[Math.min(copy.remaining(),50)]; - copy.get(bytes); - return new String(bytes, StandardCharsets.UTF_8); - } - - /** - * Main method - * - * @param args args of main - * @throws Exception when encryption/decryption failed - */ public static void main(String[] args) throws Exception { final SecretKeySpec key = new SecretKeySpec(getUTF8Bytes("1234567890123456"), "AES"); final IvParameterSpec iv = new IvParameterSpec(getUTF8Bytes("1234567890123456")); @@ -110,4 +81,27 @@ public class CipherByteBufferExample { } } + /** + * Converts String to UTF8 bytes + * + * @param input the input string + * @return UTF8 bytes + */ + private static byte[] getUTF8Bytes(String input) { + return input.getBytes(StandardCharsets.UTF_8); + } + + /** + * Converts ByteBuffer to String + * + * @param buffer input byte buffer + * @return the converted string + */ + private static String asString(ByteBuffer buffer) { + final ByteBuffer copy = buffer.duplicate(); + final byte[] bytes = new byte[Math.min(copy.remaining(),50)]; + copy.get(bytes); + return new String(bytes, StandardCharsets.UTF_8); + } + } http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/6ea9d3d8/src/main/java/org/apache/commons/crypto/examples/RandomExample.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/crypto/examples/RandomExample.java b/src/main/java/org/apache/commons/crypto/examples/RandomExample.java index 4e0f6a4..7b19b62 100644 --- a/src/main/java/org/apache/commons/crypto/examples/RandomExample.java +++ b/src/main/java/org/apache/commons/crypto/examples/RandomExample.java @@ -31,13 +31,6 @@ import org.apache.commons.crypto.random.CryptoRandomFactory; */ public class RandomExample { - /** - * Main method - * - * @param args args of main - * @throws GeneralSecurityException when encryption/decryption failed - * @throws IOException when io failed - */ public static void main(String []args) throws GeneralSecurityException, IOException { //Constructs a byte array to store random data. byte[] key = new byte[16]; http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/6ea9d3d8/src/main/java/org/apache/commons/crypto/examples/StreamExample.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/crypto/examples/StreamExample.java b/src/main/java/org/apache/commons/crypto/examples/StreamExample.java index b92ffca..372475a 100644 --- a/src/main/java/org/apache/commons/crypto/examples/StreamExample.java +++ b/src/main/java/org/apache/commons/crypto/examples/StreamExample.java @@ -36,22 +36,6 @@ import org.apache.commons.crypto.stream.CryptoOutputStream; */ public class StreamExample { - /** - * Converts String to UTF8 bytes - * - * @param input the input string - * @return UTF8 bytes - */ - private static byte[] getUTF8Bytes(String input) { - return input.getBytes(StandardCharsets.UTF_8); - } - - /** - * Main method - * - * @param args args of main - * @throws IOException when stream encryption/decryption failed - */ public static void main(String []args) throws IOException { final SecretKeySpec key = new SecretKeySpec(getUTF8Bytes("1234567890123456"),"AES"); final IvParameterSpec iv = new IvParameterSpec(getUTF8Bytes("1234567890123456")); @@ -84,4 +68,15 @@ public class StreamExample { System.out.println("Decrypted: "+new String(decryptedData, 0, decryptedLen, StandardCharsets.UTF_8)); } } + + /** + * Converts String to UTF8 bytes + * + * @param input the input string + * @return UTF8 bytes + */ + private static byte[] getUTF8Bytes(String input) { + return input.getBytes(StandardCharsets.UTF_8); + } + }
