|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| CipherKeyGen.java | - | 100% | 80% | 95% |
|
||||||||||||||
| 1 | package org.wiztools.wizcrypt; | |
| 2 | ||
| 3 | import java.io.UnsupportedEncodingException; | |
| 4 | import java.security.InvalidKeyException; | |
| 5 | import java.security.MessageDigest; | |
| 6 | import java.security.NoSuchAlgorithmException; | |
| 7 | import java.util.ResourceBundle; | |
| 8 | import javax.crypto.Cipher; | |
| 9 | import javax.crypto.NoSuchPaddingException; | |
| 10 | import javax.crypto.SecretKey; | |
| 11 | import javax.crypto.spec.SecretKeySpec; | |
| 12 | import static org.wiztools.wizcrypt.WizCryptAlgorithms.PWD_HASH; | |
| 13 | import static org.wiztools.wizcrypt.WizCryptAlgorithms.STR_ENCODE; | |
| 14 | import static org.wiztools.wizcrypt.WizCryptAlgorithms.CRYPT_ALGO; | |
| 15 | ||
| 16 | /** | |
| 17 | * This class has static methods to create <code>CipherKey</code> objects. | |
| 18 | * @see CipherKey | |
| 19 | * @see WizCrypt | |
| 20 | */ | |
| 21 | public final class CipherKeyGen{ | |
| 22 | ||
| 23 | /** Disallow public creation of instances of this class. */ | |
| 24 | 0 | private CipherKeyGen(){ |
| 25 | } | |
| 26 | ||
| 27 | 7 | private static byte[] passHash(final byte[] passKey) |
| 28 | throws NoSuchAlgorithmException, | |
| 29 | UnsupportedEncodingException{ | |
| 30 | 7 | MessageDigest md = MessageDigest.getInstance(PWD_HASH); |
| 31 | 7 | md.update(passKey); |
| 32 | 7 | byte[] raw = md.digest(); |
| 33 | 7 | return raw; |
| 34 | } | |
| 35 | ||
| 36 | 7 | private static CipherKey getCipherKey(final String keyStr, final int mode) |
| 37 | throws NoSuchAlgorithmException, | |
| 38 | UnsupportedEncodingException, | |
| 39 | InvalidKeyException, | |
| 40 | NoSuchPaddingException{ | |
| 41 | 7 | byte[] passKeyHash = null; |
| 42 | 7 | Cipher cipher = null; |
| 43 | ||
| 44 | 7 | byte[] passKey = keyStr.getBytes(STR_ENCODE); |
| 45 | ||
| 46 | 7 | SecretKey key = new SecretKeySpec(passKey, CRYPT_ALGO); |
| 47 | 7 | cipher = Cipher.getInstance(CRYPT_ALGO); |
| 48 | 7 | cipher.init(mode, key); |
| 49 | 7 | passKeyHash = CipherKeyGen.passHash(passKey); |
| 50 | ||
| 51 | 7 | CipherKey ck = new CipherKey(cipher, passKeyHash); |
| 52 | ||
| 53 | 7 | return ck; |
| 54 | } | |
| 55 | ||
| 56 | /** | |
| 57 | * This is the public API used for getting the <code>CipherKey</code> object | |
| 58 | * used in <code>WizCrypt</code> public APIs. | |
| 59 | * | |
| 60 | * @param keyStr The password used for creating the cipher. | |
| 61 | * @throws NoSuchAlgorithmException When the Algorithm used by WizCrypt (RC4) is not found in JVM. This is highly unlikely, because SUN JVM has this. | |
| 62 | * @throws UnsupportedEncodingException Password provided is encoded using UTF-8. If UTF-8 encoder is not available in JVM, this exception is thrown. This exception is not likely to happen. | |
| 63 | * @throws InvalidKeyException The key should be of specific size. If this size limits are not met, <code>InvalidKeyException</code> exception is thrown. | |
| 64 | * @throws NoSuchPaddingException This exception is thrown when a particular padding mechanism is requested but is not available in the environment. This also is also highly unlikely to happen. | |
| 65 | * @return Returns the initialized <code>CipherKey</code> object for encryption. | |
| 66 | * @see WizCrypt#encrypt(InputStream is, OutputStream os, CipherKey ce) | |
| 67 | * @see CipherKey | |
| 68 | */ | |
| 69 | 3 | public static CipherKey getCipherKeyForEncrypt(final String keyStr) |
| 70 | throws NoSuchAlgorithmException, | |
| 71 | UnsupportedEncodingException, | |
| 72 | InvalidKeyException, | |
| 73 | NoSuchPaddingException{ | |
| 74 | 3 | return getCipherKey(keyStr, Cipher.ENCRYPT_MODE); |
| 75 | } | |
| 76 | ||
| 77 | /** | |
| 78 | * This is the public API used for getting the <code>CipherKey</code> object | |
| 79 | * used in <code>WizCrypt</code> public APIs. | |
| 80 | * | |
| 81 | * @param keyStr The password used for creating the cipher. | |
| 82 | * @throws NoSuchAlgorithmException When the Algorithm used by WizCrypt (RC4) is not found in JVM. This is highly unlikely, because SUN JVM has this. | |
| 83 | * @throws UnsupportedEncodingException Password provided is encoded using UTF-8. If UTF-8 encoder is not available in JVM, this exception is thrown. This exception is not likely to happen. | |
| 84 | * @throws InvalidKeyException The key should be of specific size. If this size limits are not met, <code>InvalidKeyException</code> exception is thrown. | |
| 85 | * @throws NoSuchPaddingException This exception is thrown when a particular padding mechanism is requested but is not available in the environment. This also is also highly unlikely to happen. | |
| 86 | * @return Returns the initialized <code>CipherKey</code> object for decryption. | |
| 87 | * @see WizCrypt#decrypt(InputStream is, OutputStream os, CipherKey ce) | |
| 88 | * @see CipherKey | |
| 89 | */ | |
| 90 | 4 | public static CipherKey getCipherKeyForDecrypt(final String keyStr) |
| 91 | throws NoSuchAlgorithmException, | |
| 92 | UnsupportedEncodingException, | |
| 93 | InvalidKeyException, | |
| 94 | NoSuchPaddingException{ | |
| 95 | 4 | return getCipherKey(keyStr, Cipher.DECRYPT_MODE); |
| 96 | } | |
| 97 | } |
|
||||||||||