Skip to content

Commit 61598da

Browse files
committed
Refactoring: naming conventions
1 parent a189a4a commit 61598da

11 files changed

Lines changed: 64 additions & 65 deletions

File tree

crypto-hash/src/main/java/de/dominikschadow/javasecurity/hash/MD5.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,12 @@
1818
package de.dominikschadow.javasecurity.hash;
1919

2020
import com.google.common.io.BaseEncoding;
21-
import com.google.common.primitives.Bytes;
2221
import org.slf4j.Logger;
2322
import org.slf4j.LoggerFactory;
2423

2524
import java.io.UnsupportedEncodingException;
2625
import java.security.MessageDigest;
2726
import java.security.NoSuchAlgorithmException;
28-
import java.security.SecureRandom;
2927

3028
/**
3129
* MD5 hashing sample with plain Java. No salt and no iterations are used to calculate the hash
@@ -36,7 +34,7 @@
3634
* @author Dominik Schadow
3735
*/
3836
public class MD5 {
39-
private static final Logger logger = LoggerFactory.getLogger(MD5.class);
37+
private static final Logger LOGGER = LoggerFactory.getLogger(MD5.class);
4038
private static final String ALGORITHM = "MD5";
4139

4240
public static void main(String[] args) {
@@ -47,9 +45,9 @@ public static void main(String[] args) {
4745
byte[] hash = hs.calculateHash(password);
4846
boolean correct = hs.verifyPassword(hash, password);
4947

50-
logger.info("Entered password is correct: {}", correct);
48+
LOGGER.info("Entered password is correct: {}", correct);
5149
} catch (NoSuchAlgorithmException | UnsupportedEncodingException ex) {
52-
logger.error(ex.getMessage(), ex);
50+
LOGGER.error(ex.getMessage(), ex);
5351
}
5452
}
5553

@@ -69,8 +67,8 @@ private boolean verifyPassword(byte[] originalHash, String password) throws
6967
NoSuchAlgorithmException, UnsupportedEncodingException {
7068
byte[] comparisonHash = calculateHash(password);
7169

72-
logger.info("hash 1: {}", BaseEncoding.base64().encode(originalHash));
73-
logger.info("hash 2: {}", BaseEncoding.base64().encode(comparisonHash));
70+
LOGGER.info("hash 1: {}", BaseEncoding.base64().encode(originalHash));
71+
LOGGER.info("hash 2: {}", BaseEncoding.base64().encode(comparisonHash));
7472

7573
return comparePasswords(originalHash, comparisonHash);
7674
}

crypto-hash/src/main/java/de/dominikschadow/javasecurity/hash/PBKDF2.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
* @author Dominik Schadow
3737
*/
3838
public class PBKDF2 {
39-
private static final Logger logger = LoggerFactory.getLogger(PBKDF2.class);
39+
private static final Logger LOGGER = LoggerFactory.getLogger(PBKDF2.class);
4040
private static final String ALGORITHM = "PBKDF2WithHmacSHA1";
4141
private static final int ITERATIONS = 10000;
4242
// salt size at least 32 byte
@@ -51,15 +51,15 @@ public static void main(String[] args) {
5151
SecretKeyFactory skf = SecretKeyFactory.getInstance(ALGORITHM);
5252
byte[] salt = hs.generateSalt();
5353

54-
logger.info("Password {}, hash algorithm {}, hash size {}, iterations {}, salt {}", String.valueOf
55-
(password), ALGORITHM, HASH_SIZE, ITERATIONS, BaseEncoding.base64().encode(salt));
54+
LOGGER.info("Password {}, hash algorithm {}, hash size {}, iterations {}, salt {}", String.valueOf
55+
(password), ALGORITHM, HASH_SIZE, ITERATIONS, BaseEncoding.base64().encode(salt));
5656

5757
byte[] hash = hs.calculateHash(skf, password, salt);
5858
boolean correct = hs.verifyPassword(skf, hash, password, salt);
5959

60-
logger.info("Entered password is correct: {}", correct);
60+
LOGGER.info("Entered password is correct: {}", correct);
6161
} catch (NoSuchAlgorithmException | InvalidKeySpecException ex) {
62-
logger.error(ex.getMessage(), ex);
62+
LOGGER.error(ex.getMessage(), ex);
6363
}
6464
}
6565

@@ -81,8 +81,8 @@ private boolean verifyPassword(SecretKeyFactory skf, byte[] originalHash, char[]
8181
InvalidKeySpecException {
8282
byte[] comparisonHash = calculateHash(skf, password, salt);
8383

84-
logger.info("hash 1: {}", BaseEncoding.base64().encode(originalHash));
85-
logger.info("hash 2: {}", BaseEncoding.base64().encode(comparisonHash));
84+
LOGGER.info("hash 1: {}", BaseEncoding.base64().encode(originalHash));
85+
LOGGER.info("hash 2: {}", BaseEncoding.base64().encode(comparisonHash));
8686

8787
return comparePasswords(originalHash, comparisonHash);
8888
}

crypto-hash/src/main/java/de/dominikschadow/javasecurity/hash/SHA512.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
* @author Dominik Schadow
3737
*/
3838
public class SHA512 {
39-
private static final Logger logger = LoggerFactory.getLogger(SHA512.class);
39+
private static final Logger LOGGER = LoggerFactory.getLogger(SHA512.class);
4040
private static final String ALGORITHM = "SHA-512";
4141
private static final int ITERATIONS = 1000000;
4242
private static final int SALT_SIZE = 64;
@@ -48,15 +48,15 @@ public static void main(String[] args) {
4848
try {
4949
byte[] salt = hs.generateSalt();
5050

51-
logger.info("Password {}. hash algorithm {}, iterations {}, salt {}", password, ALGORITHM, ITERATIONS,
51+
LOGGER.info("Password {}. hash algorithm {}, iterations {}, salt {}", password, ALGORITHM, ITERATIONS,
5252
BaseEncoding.base64().encode(salt));
5353

5454
byte[] hash = hs.calculateHash(password, salt);
5555
boolean correct = hs.verifyPassword(hash, password, salt);
5656

57-
logger.info("Entered password is correct: {}", correct);
57+
LOGGER.info("Entered password is correct: {}", correct);
5858
} catch (NoSuchAlgorithmException | UnsupportedEncodingException ex) {
59-
logger.error(ex.getMessage(), ex);
59+
LOGGER.error(ex.getMessage(), ex);
6060
}
6161
}
6262

@@ -87,8 +87,8 @@ private boolean verifyPassword(byte[] originalHash, String password, byte[] salt
8787
NoSuchAlgorithmException, UnsupportedEncodingException {
8888
byte[] comparisonHash = calculateHash(password, salt);
8989

90-
logger.info("hash 1: {}", BaseEncoding.base64().encode(originalHash));
91-
logger.info("hash 2: {}", BaseEncoding.base64().encode(comparisonHash));
90+
LOGGER.info("hash 1: {}", BaseEncoding.base64().encode(originalHash));
91+
LOGGER.info("hash 2: {}", BaseEncoding.base64().encode(comparisonHash));
9292

9393
return comparePasswords(originalHash, comparisonHash);
9494
}

crypto-java/src/main/java/de/dominikschadow/javasecurity/asymmetric/DSA.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
* @author Dominik Schadow
3636
*/
3737
public class DSA {
38-
private static final Logger logger = LoggerFactory.getLogger(DSA.class);
38+
private static final Logger LOGGER = LoggerFactory.getLogger(DSA.class);
3939
private static final String ALGORITHM = "SHA1withDSA";
4040
private static final String KEYSTORE_PATH = "/samples.ks";
4141

@@ -57,7 +57,7 @@ public static void main(String[] args) {
5757
aes.printReadableMessages(initialText, signature, valid);
5858
} catch (NoSuchAlgorithmException | SignatureException | KeyStoreException | CertificateException |
5959
UnrecoverableKeyException | InvalidKeyException | IOException ex) {
60-
logger.error(ex.getMessage(), ex);
60+
LOGGER.error(ex.getMessage(), ex);
6161
}
6262
}
6363

@@ -109,9 +109,9 @@ private boolean verify(PublicKey publicKey, byte[] signature, String initialText
109109
}
110110

111111
private void printReadableMessages(String initialText, byte[] signature, boolean valid) {
112-
logger.info("initialText: {}", initialText);
113-
logger.info("signature as byte[]: {}", new String(signature));
114-
logger.info("signature as Base64: {}", BaseEncoding.base64().encode(signature));
115-
logger.info("signature valid: {}", valid);
112+
LOGGER.info("initialText: {}", initialText);
113+
LOGGER.info("signature as byte[]: {}", new String(signature));
114+
LOGGER.info("signature as Base64: {}", BaseEncoding.base64().encode(signature));
115+
LOGGER.info("signature valid: {}", valid);
116116
}
117117
}

crypto-java/src/main/java/de/dominikschadow/javasecurity/asymmetric/RSA.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
* @author Dominik Schadow
4040
*/
4141
public class RSA {
42-
private static final Logger logger = LoggerFactory.getLogger(RSA.class);
42+
private static final Logger LOGGER = LoggerFactory.getLogger(RSA.class);
4343
private static final String ALGORITHM = "RSA";
4444
private static final String KEYSTORE_PATH = "/samples.ks";
4545

@@ -61,7 +61,7 @@ public static void main(String[] args) {
6161
aes.printReadableMessages(initialText, ciphertext, plaintext);
6262
} catch (NoSuchPaddingException | NoSuchAlgorithmException | IllegalBlockSizeException | BadPaddingException |
6363
KeyStoreException | CertificateException | UnrecoverableKeyException | InvalidKeyException | IOException ex) {
64-
logger.error(ex.getMessage(), ex);
64+
LOGGER.error(ex.getMessage(), ex);
6565
}
6666
}
6767

@@ -113,9 +113,9 @@ private byte[] decrypt(PrivateKey privateKey, byte[] ciphertext) throws NoSuchPa
113113
}
114114

115115
private void printReadableMessages(String initialText, byte[] ciphertext, byte[] plaintext) {
116-
logger.info("initialText: {}", initialText);
117-
logger.info("cipherText as byte[]: {}", new String(ciphertext));
118-
logger.info("cipherText as Base64: {}", BaseEncoding.base64().encode(ciphertext));
119-
logger.info("plaintext: {}", new String(plaintext));
116+
LOGGER.info("initialText: {}", initialText);
117+
LOGGER.info("cipherText as byte[]: {}", new String(ciphertext));
118+
LOGGER.info("cipherText as Base64: {}", BaseEncoding.base64().encode(ciphertext));
119+
LOGGER.info("plaintext: {}", new String(plaintext));
120120
}
121121
}

crypto-java/src/main/java/de/dominikschadow/javasecurity/symmetric/AES.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
* @author Dominik Schadow
4444
*/
4545
public class AES {
46-
private static final Logger logger = LoggerFactory.getLogger(AES.class);
46+
private static final Logger LOGGER = LoggerFactory.getLogger(AES.class);
4747
private static final String ALGORITHM = "AES/CBC/PKCS5Padding";
4848
private static final String KEYSTORE_PATH = "/samples.ks";
4949
/** Non-secret initialization vector with 16 bytes (publicly exchanged between participants), may be a random number changed every time or a counter. */
@@ -68,7 +68,7 @@ public static void main(String[] args) {
6868
} catch (NoSuchPaddingException | NoSuchAlgorithmException | IllegalBlockSizeException | BadPaddingException |
6969
KeyStoreException | CertificateException | UnrecoverableKeyException | InvalidAlgorithmParameterException |
7070
InvalidKeyException | IOException ex) {
71-
logger.error(ex.getMessage(), ex);
71+
LOGGER.error(ex.getMessage(), ex);
7272
}
7373
}
7474

@@ -112,9 +112,9 @@ private byte[] decrypt(SecretKeySpec secretKeySpec, IvParameterSpec initialVecto
112112
}
113113

114114
private void printReadableMessages(String initialText, byte[] ciphertext, byte[] plaintext) {
115-
logger.info("initialText: {}", initialText);
116-
logger.info("cipherText as byte[]: {}", new String(ciphertext));
117-
logger.info("cipherText as Base64: {}", BaseEncoding.base64().encode(ciphertext));
118-
logger.info("plaintext: {}", new String(plaintext));
115+
LOGGER.info("initialText: {}", initialText);
116+
LOGGER.info("cipherText as byte[]: {}", new String(ciphertext));
117+
LOGGER.info("cipherText as Base64: {}", BaseEncoding.base64().encode(ciphertext));
118+
LOGGER.info("plaintext: {}", new String(plaintext));
119119
}
120120
}

crypto-keyczar/src/main/java/de/dominikschadow/javasecurity/asymmetric/DSASignatureSample.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
* @author Dominik Schadow
3030
*/
3131
public class DSASignatureSample {
32-
private static final Logger logger = LoggerFactory.getLogger(DSASignatureSample.class);
32+
private static final Logger LOGGER = LoggerFactory.getLogger(DSASignatureSample.class);
3333
private static final String KEYSET_PATH = "crypto-keyczar/src/main/resources/key-sets/sign";
3434

3535
public static void main(String[] args) {
@@ -41,7 +41,7 @@ public static void main(String[] args) {
4141

4242
res.printReadableMessages(initialText, signature, valid);
4343
} catch (KeyczarException ex) {
44-
logger.error(ex.getMessage(), ex);
44+
LOGGER.error(ex.getMessage(), ex);
4545
}
4646
}
4747

@@ -60,8 +60,8 @@ private boolean verify(String initialText, String signature) throws KeyczarExcep
6060
}
6161

6262
private void printReadableMessages(String initialText, String signature, boolean valid) {
63-
logger.info("initialText: {}", initialText);
64-
logger.info("signature as Base64: {}", signature);
65-
logger.info("signature valid: {}", valid);
63+
LOGGER.info("initialText: {}", initialText);
64+
LOGGER.info("signature as Base64: {}", signature);
65+
LOGGER.info("signature valid: {}", valid);
6666
}
6767
}

crypto-keyczar/src/main/java/de/dominikschadow/javasecurity/asymmetric/RSAEncryptionSample.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* @author Dominik Schadow
2929
*/
3030
public class RSAEncryptionSample {
31-
private static final Logger logger = LoggerFactory.getLogger(RSAEncryptionSample.class);
31+
private static final Logger LOGGER = LoggerFactory.getLogger(RSAEncryptionSample.class);
3232
private static final String KEYSET_PATH = "crypto-keyczar/src/main/resources/key-sets/encrypt/asymmetric";
3333

3434
public static void main(String[] args) {
@@ -40,7 +40,7 @@ public static void main(String[] args) {
4040

4141
res.printReadableMessages(initialText, ciphertext, plaintext);
4242
} catch (KeyczarException ex) {
43-
logger.error(ex.getMessage(), ex);
43+
LOGGER.error(ex.getMessage(), ex);
4444
}
4545
}
4646

@@ -66,8 +66,8 @@ private String decrypt(String ciphertext) throws KeyczarException {
6666
}
6767

6868
private void printReadableMessages(String initialText, String ciphertext, String plaintext) {
69-
logger.info("initialText: {}", initialText);
70-
logger.info("cipherText as Base64: {}", ciphertext);
71-
logger.info("plaintext: {}", plaintext);
69+
LOGGER.info("initialText: {}", initialText);
70+
LOGGER.info("cipherText as Base64: {}", ciphertext);
71+
LOGGER.info("plaintext: {}", plaintext);
7272
}
7373
}

crypto-keyczar/src/main/java/de/dominikschadow/javasecurity/symmetric/AESEncryptionSample.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* @author Dominik Schadow
2929
*/
3030
public class AESEncryptionSample {
31-
private static final Logger logger = LoggerFactory.getLogger(AESEncryptionSample.class);
31+
private static final Logger LOGGER = LoggerFactory.getLogger(AESEncryptionSample.class);
3232
private static final String KEYSET_PATH = "crypto-keyczar/src/main/resources/key-sets/encrypt/symmetric";
3333

3434
public static void main(String[] args) {
@@ -40,7 +40,7 @@ public static void main(String[] args) {
4040

4141
res.printReadableMessages(initialText, ciphertext, plaintext);
4242
} catch (KeyczarException ex) {
43-
logger.error(ex.getMessage(), ex);
43+
LOGGER.error(ex.getMessage(), ex);
4444
}
4545
}
4646

@@ -66,8 +66,8 @@ private String decrypt(String ciphertext) throws KeyczarException {
6666
}
6767

6868
private void printReadableMessages(String initialText, String ciphertext, String plaintext) {
69-
logger.info("initialText: {}", initialText);
70-
logger.info("cipherText as Base64: {}", ciphertext);
71-
logger.info("plaintext: {}", plaintext);
69+
LOGGER.info("initialText: {}", initialText);
70+
LOGGER.info("cipherText as Base64: {}", ciphertext);
71+
LOGGER.info("plaintext: {}", plaintext);
7272
}
7373
}

crypto-shiro/src/main/java/de/dominikschadow/javasecurity/hash/SHA512HashSample.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
* @author Dominik Schadow
3535
*/
3636
public class SHA512HashSample {
37-
private static final Logger logger = LoggerFactory.getLogger(SHA512HashSample.class);
37+
private static final Logger LOGGER = LoggerFactory.getLogger(SHA512HashSample.class);
3838
/** Nothing up my sleeve number as private salt, not good for production. */
3939
private static final byte[] PRIVATE_SALT_BYTES = {3, 1, 4, 1, 5, 9, 2, 6, 5};
4040
private static final int ITERATIONS = 1000000;
@@ -46,7 +46,7 @@ public static void main(String[] args) {
4646
Hash hash = hs.calculateHash(password);
4747
boolean correct = hs.verifyPassword(hash.getBytes(), hash.getSalt(), password);
4848

49-
logger.info("Entered password is correct: {}", correct);
49+
LOGGER.info("Entered password is correct: {}", correct);
5050
}
5151

5252
private Hash calculateHash(String password) {
@@ -61,7 +61,8 @@ private Hash calculateHash(String password) {
6161

6262
Hash hash = hashService.computeHash(builder.build());
6363

64-
logger.info("Hash algorithm {}, iterations {}, public salt {}", hash.getAlgorithmName(), hash.getIterations(), hash.getSalt());
64+
LOGGER.info("Hash algorithm {}, iterations {}, public salt {}", hash.getAlgorithmName(), hash.getIterations()
65+
, hash.getSalt());
6566

6667
return hash;
6768
}
@@ -78,9 +79,9 @@ private boolean verifyPassword(byte[] originalHash, ByteSource publicSalt, Strin
7879

7980
Hash comparisonHash = hashService.computeHash(builder.build());
8081

81-
logger.info("password: {}", password);
82-
logger.info("1 hash: {}", Base64.encodeToString(originalHash));
83-
logger.info("2 hash: {}", comparisonHash.toBase64());
82+
LOGGER.info("password: {}", password);
83+
LOGGER.info("1 hash: {}", Base64.encodeToString(originalHash));
84+
LOGGER.info("2 hash: {}", comparisonHash.toBase64());
8485

8586
return Arrays.equals(originalHash, comparisonHash.getBytes());
8687
}

0 commit comments

Comments
 (0)