Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
dschadow committed Dec 14, 2016
1 parent 6ce3e12 commit bc21e80
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
@RequestMapping(value = "/contacts")
public class ContactController {
private static final Logger log = LoggerFactory.getLogger(ContactController.class);
private ContactService contactService;
private final ContactService contactService;

public ContactController(ContactService contactService) {
this.contactService = contactService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
*/
@Service
public class ContactService {
private JdbcTemplate jdbcTemplate;
private final JdbcTemplate jdbcTemplate;

public ContactService(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private static void sign() {
final char[] keyPassword = "asymmetric-sample-dsa".toCharArray();

try {
KeyStore ks = loadKeystore(KEYSTORE_PATH, keystorePassword);
KeyStore ks = loadKeystore(keystorePassword);
PrivateKey privateKey = loadPrivateKey(ks, keyAlias, keyPassword);
PublicKey publicKey = loadPublicKey(ks, keyAlias);

Expand All @@ -71,9 +71,9 @@ private static void sign() {
}
}

private static KeyStore loadKeystore(String keystorePath, char[] keystorePassword) throws KeyStoreException,
private static KeyStore loadKeystore(char[] keystorePassword) throws KeyStoreException,
CertificateException, NoSuchAlgorithmException, IOException {
try (InputStream keystoreStream = DSA.class.getResourceAsStream(keystorePath)) {
try (InputStream keystoreStream = DSA.class.getResourceAsStream(KEYSTORE_PATH)) {
KeyStore ks = KeyStore.getInstance("JCEKS");
ks.load(keystoreStream, keystorePassword);
return ks;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private static void encrypt() {
final char[] keyPassword = "asymmetric-sample-rsa".toCharArray();

try {
KeyStore ks = loadKeystore(KEYSTORE_PATH, keystorePassword);
KeyStore ks = loadKeystore(keystorePassword);
PrivateKey privateKey = loadPrivateKey(ks, keyAlias, keyPassword);
PublicKey publicKey = loadPublicKey(ks, keyAlias);

Expand All @@ -76,9 +76,9 @@ private static void encrypt() {
}
}

private static KeyStore loadKeystore(String keystorePath, char[] keystorePassword) throws KeyStoreException,
private static KeyStore loadKeystore(char[] keystorePassword) throws KeyStoreException,
CertificateException, NoSuchAlgorithmException, IOException {
try (InputStream keystoreStream = RSA.class.getResourceAsStream(keystorePath)) {
try (InputStream keystoreStream = RSA.class.getResourceAsStream(KEYSTORE_PATH)) {
KeyStore ks = KeyStore.getInstance("JCEKS");
ks.load(keystoreStream, keystorePassword);
return ks;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private void encrypt() {

try {
cipher = Cipher.getInstance(ALGORITHM);
KeyStore ks = loadKeystore(KEYSTORE_PATH, keystorePassword);
KeyStore ks = loadKeystore(keystorePassword);
Key key = loadKey(ks, keyAlias, keyPassword);
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getEncoded(), "AES");
byte[] ciphertext = encrypt(secretKeySpec, initialText);
Expand All @@ -79,9 +79,9 @@ private void encrypt() {
}
}

private KeyStore loadKeystore(String keystorePath, char[] keystorePassword) throws KeyStoreException,
private KeyStore loadKeystore(char[] keystorePassword) throws KeyStoreException,
CertificateException, NoSuchAlgorithmException, IOException {
try (InputStream keystoreStream = getClass().getResourceAsStream(keystorePath)) {
try (InputStream keystoreStream = getClass().getResourceAsStream(KEYSTORE_PATH)) {
KeyStore ks = KeyStore.getInstance("JCEKS");
ks.load(keystoreStream, keystorePassword);

Expand All @@ -98,15 +98,15 @@ private static Key loadKey(KeyStore ks, String keyAlias, char[] keyPassword) thr
return ks.getKey(keyAlias, keyPassword);
}

private byte[] encrypt(SecretKeySpec secretKeySpec, String initialText) throws NoSuchPaddingException,
NoSuchAlgorithmException, UnsupportedEncodingException, BadPaddingException,
IllegalBlockSizeException, InvalidAlgorithmParameterException, InvalidKeyException {
private byte[] encrypt(SecretKeySpec secretKeySpec, String initialText) throws
UnsupportedEncodingException, BadPaddingException,
IllegalBlockSizeException, InvalidKeyException {
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
return cipher.doFinal(initialText.getBytes("UTF-8"));
}

private byte[] decrypt(SecretKeySpec secretKeySpec, byte[] ciphertext) throws NoSuchPaddingException,
NoSuchAlgorithmException, BadPaddingException, IllegalBlockSizeException,
private byte[] decrypt(SecretKeySpec secretKeySpec, byte[] ciphertext) throws
BadPaddingException, IllegalBlockSizeException,
InvalidAlgorithmParameterException, InvalidKeyException {
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, new IvParameterSpec(cipher.getIV()));
return cipher.doFinal(ciphertext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static void main(String[] args) {
final char[] keyPassword = "symmetric-sample".toCharArray();

try {
KeyStore ks = loadKeystore(KEYSTORE_PATH, keystorePassword);
KeyStore ks = loadKeystore(keystorePassword);
Key key = loadKey(ks, keyAlias, keyPassword);
byte[] ciphertext = encrypt(key, CodecSupport.toBytes(initialText));
byte[] plaintext = decrypt(key, ciphertext);
Expand All @@ -62,8 +62,8 @@ public static void main(String[] args) {
}
}

private static KeyStore loadKeystore(String keystorePath, char[] keystorePassword) throws KeyStoreException, CertificateException, NoSuchAlgorithmException, IOException {
InputStream keystoreStream = AESEncryption.class.getResourceAsStream(keystorePath);
private static KeyStore loadKeystore(char[] keystorePassword) throws KeyStoreException, CertificateException, NoSuchAlgorithmException, IOException {
InputStream keystoreStream = AESEncryption.class.getResourceAsStream(KEYSTORE_PATH);

KeyStore ks = KeyStore.getInstance("JCEKS");
ks.load(keystoreStream, keystorePassword);
Expand Down

0 comments on commit bc21e80

Please sign in to comment.