Skip to content

Commit 2a66d69

Browse files
committed
Imroved the debug logging.
1 parent 7ad0e99 commit 2a66d69

File tree

6 files changed

+294
-269
lines changed

6 files changed

+294
-269
lines changed

src/main/java/ro/kuberam/libs/java/crypto/digest/Hash.java

Lines changed: 65 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -39,75 +39,72 @@
3939
/**
4040
* Implements the crypto:hash() function.
4141
*
42-
* @author <a href="mailto:[email protected]">Claudius Teodorescu</a>
42+
* @author <a href="mailto:[email protected]">Claudius
43+
* Teodorescu</a>
4344
*/
4445
public class Hash {
4546

46-
private static final Logger LOG = LogManager.getLogger(Hash.class);
47-
48-
public static String hashString(final String data, final String algorithm) throws CryptoException {
49-
return hashString(data, algorithm, null);
50-
}
51-
52-
public static String hashString(final String data, final String algorithm, final @Nullable String format) throws CryptoException {
53-
54-
// TODO: validate the format
55-
final String actualFormat = Optional.ofNullable(format)
56-
.filter(str -> !str.isEmpty())
57-
.orElse("base64"); // default to Base64
58-
59-
final MessageDigest messageDigester = getMessageDigester(algorithm);
60-
messageDigester.update(data.getBytes(StandardCharsets.UTF_8));
61-
62-
final byte[] resultBytes = messageDigester.digest();
63-
64-
if (actualFormat.equals("base64")) {
65-
return Base64.getEncoder().encodeToString(resultBytes);
66-
} else {
67-
return HexString.fromBytes(resultBytes);
68-
}
69-
}
70-
71-
public static String hashBinary(final InputStream data, final String algorithm) throws CryptoException, IOException {
72-
return hashBinary(data, algorithm, null);
73-
}
74-
75-
public static String hashBinary(final InputStream data, final String algorithm, @Nullable final String format) throws CryptoException, IOException {
76-
77-
// TODO: validate the format
78-
final String actualFormat = Optional.ofNullable(format)
79-
.filter(str -> !str.isEmpty())
80-
.orElse("base64"); // default to Base64
81-
82-
final byte[] resultBytes;
83-
final MessageDigest messageDigester = getMessageDigester(algorithm);
84-
85-
final byte[] buf = new byte[Buffer.TRANSFER_SIZE];
86-
int read = -1;
87-
while((read = data.read(buf)) > -1) {
88-
messageDigester.update(buf, 0, read);
89-
}
90-
resultBytes = messageDigester.digest();
91-
92-
final String result;
93-
if (actualFormat.equals("base64")) {
94-
result = Base64.getEncoder().encodeToString(resultBytes);
95-
} else {
96-
result = HexString.fromBytes(resultBytes);
97-
}
98-
99-
if (LOG.isDebugEnabled()) {
100-
LOG.debug("hash value is: '" + result);
101-
}
102-
103-
return result;
104-
}
105-
106-
private static MessageDigest getMessageDigester(final String algorithm) throws CryptoException {
107-
try {
108-
return MessageDigest.getInstance(algorithm);
109-
} catch (final NoSuchAlgorithmException e) {
110-
throw new CryptoException(CryptoError.UNKNOWN_ALGORITH, e);
111-
}
112-
}
47+
private static final Logger LOG = LogManager.getLogger(Hash.class);
48+
49+
public static String hashString(final String data, final String algorithm) throws CryptoException {
50+
return hashString(data, algorithm, null);
51+
}
52+
53+
public static String hashString(final String data, final String algorithm, final @Nullable String format)
54+
throws CryptoException {
55+
56+
// TODO: validate the format
57+
final String actualFormat = Optional.ofNullable(format).filter(str -> !str.isEmpty()).orElse("base64");
58+
59+
final MessageDigest messageDigester = getMessageDigester(algorithm);
60+
messageDigester.update(data.getBytes(StandardCharsets.UTF_8));
61+
62+
final byte[] resultBytes = messageDigester.digest();
63+
64+
if (actualFormat.equals("base64")) {
65+
return Base64.getEncoder().encodeToString(resultBytes);
66+
} else {
67+
return HexString.fromBytes(resultBytes);
68+
}
69+
}
70+
71+
public static String hashBinary(final InputStream data, final String algorithm)
72+
throws CryptoException, IOException {
73+
return hashBinary(data, algorithm, null);
74+
}
75+
76+
public static String hashBinary(final InputStream data, final String algorithm, @Nullable final String format)
77+
throws CryptoException, IOException {
78+
79+
// TODO: validate the format
80+
final String actualFormat = Optional.ofNullable(format).filter(str -> !str.isEmpty()).orElse("base64");
81+
82+
final byte[] resultBytes;
83+
final MessageDigest messageDigester = getMessageDigester(algorithm);
84+
85+
final byte[] buf = new byte[Buffer.TRANSFER_SIZE];
86+
int read = -1;
87+
while ((read = data.read(buf)) > -1) {
88+
messageDigester.update(buf, 0, read);
89+
}
90+
resultBytes = messageDigester.digest();
91+
92+
final String result;
93+
if (actualFormat.equals("base64")) {
94+
result = Base64.getEncoder().encodeToString(resultBytes);
95+
} else {
96+
result = HexString.fromBytes(resultBytes);
97+
}
98+
LOG.debug("hash value is = {}", () -> result);
99+
100+
return result;
101+
}
102+
103+
private static MessageDigest getMessageDigester(final String algorithm) throws CryptoException {
104+
try {
105+
return MessageDigest.getInstance(algorithm);
106+
} catch (final NoSuchAlgorithmException e) {
107+
throw new CryptoException(CryptoError.UNKNOWN_ALGORITH, e);
108+
}
109+
}
113110
}

src/main/java/ro/kuberam/libs/java/crypto/digest/Hmac.java

Lines changed: 70 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -34,115 +34,103 @@
3434

3535
import org.apache.logging.log4j.LogManager;
3636
import org.apache.logging.log4j.Logger;
37+
3738
import ro.kuberam.libs.java.crypto.CryptoError;
3839
import ro.kuberam.libs.java.crypto.CryptoException;
3940
import ro.kuberam.libs.java.crypto.ExpathCryptoModule;
4041
import ro.kuberam.libs.java.crypto.utils.Buffer;
4142

4243
public class Hmac {
4344

44-
private static final Logger LOG = LogManager.getLogger(Hmac.class);
45-
46-
public static String hmac(final byte[] data, final byte[] secretKey, final String algorithm, @Nullable final String format) throws CryptoException {
47-
48-
// TODO: validate the format
49-
final String actualFormat = Optional.ofNullable(format)
50-
.filter(str -> !str.isEmpty())
51-
.orElse("base64"); // default to Base64
52-
53-
if (LOG.isDebugEnabled()) {
54-
LOG.debug("secretKey = " + secretKey);
55-
}
56-
57-
final byte[] resultBytes = hmac(data, secretKey, algorithm);
45+
private static final Logger LOG = LogManager.getLogger(Hmac.class);
5846

59-
final String result;
60-
if (actualFormat.equals("base64")) {
61-
result = Base64.getEncoder().encodeToString(resultBytes);
62-
} else {
63-
result = DatatypeConverter.printHexBinary(resultBytes).toLowerCase();
64-
}
47+
public static String hmac(final byte[] data, final byte[] secretKey, final String algorithm,
48+
@Nullable final String format) throws CryptoException {
6549

66-
if (LOG.isDebugEnabled()) {
67-
LOG.debug("result = " + result);
68-
}
50+
// TODO: validate the format
51+
final String actualFormat = Optional.ofNullable(format).filter(str -> !str.isEmpty()).orElse("base64");
52+
LOG.debug("secretKey = {}", () -> secretKey);
6953

70-
return result;
71-
}
54+
final byte[] resultBytes = hmac(data, secretKey, algorithm);
7255

73-
public static String hmac(final InputStream data, final byte[] secretKey, final String algorithm, @Nullable final String format) throws CryptoException, IOException {
56+
final String result;
57+
if (actualFormat.equals("base64")) {
58+
result = Base64.getEncoder().encodeToString(resultBytes);
59+
} else {
60+
result = DatatypeConverter.printHexBinary(resultBytes).toLowerCase();
61+
}
62+
LOG.debug("result = {}", () -> result);
7463

75-
// TODO: validate the format
76-
final String actualFormat = Optional.ofNullable(format)
77-
.filter(str -> !str.isEmpty())
78-
.orElse("base64"); // default to Base64
64+
return result;
65+
}
7966

80-
if (LOG.isDebugEnabled()) {
81-
LOG.debug("secretKey = " + secretKey);
82-
}
67+
public static String hmac(final InputStream data, final byte[] secretKey, final String algorithm,
68+
@Nullable final String format) throws CryptoException, IOException {
8369

84-
final byte[] resultBytes = hmac(data, secretKey, algorithm);
70+
// TODO: validate the format
71+
final String actualFormat = Optional.ofNullable(format).filter(str -> !str.isEmpty()).orElse("base64");
72+
LOG.debug("secretKey = {}", () -> secretKey);
8573

86-
final String result;
87-
if (actualFormat.equals("base64")) {
88-
result = Base64.getEncoder().encodeToString(resultBytes);
89-
} else {
90-
result = DatatypeConverter.printHexBinary(resultBytes).toLowerCase();
91-
}
74+
final byte[] resultBytes = hmac(data, secretKey, algorithm);
9275

93-
if (LOG.isDebugEnabled()) {
94-
LOG.debug("result = " + result);
95-
}
76+
final String result;
77+
if (actualFormat.equals("base64")) {
78+
result = Base64.getEncoder().encodeToString(resultBytes);
79+
} else {
80+
result = DatatypeConverter.printHexBinary(resultBytes).toLowerCase();
81+
}
82+
LOG.debug("result = {}", () -> result);
9683

97-
return result;
98-
}
84+
return result;
85+
}
9986

100-
public static byte[] hmac(final byte[] data, final byte[] secretKey, String algorithm) throws CryptoException {
101-
final Map<String, String> javaStandardAlgorithmNames = ExpathCryptoModule.javaStandardAlgorithmNames;
87+
public static byte[] hmac(final byte[] data, final byte[] secretKey, String algorithm) throws CryptoException {
88+
final Map<String, String> javaStandardAlgorithmNames = ExpathCryptoModule.javaStandardAlgorithmNames;
10289

103-
if (javaStandardAlgorithmNames.containsKey(algorithm)) {
104-
algorithm = javaStandardAlgorithmNames.get(algorithm);
105-
}
90+
if (javaStandardAlgorithmNames.containsKey(algorithm)) {
91+
algorithm = javaStandardAlgorithmNames.get(algorithm);
92+
}
10693

107-
final SecretKeySpec signingKey = new SecretKeySpec(secretKey, algorithm);
94+
final SecretKeySpec signingKey = new SecretKeySpec(secretKey, algorithm);
10895

109-
try {
110-
final Mac mac = Mac.getInstance(algorithm);
111-
mac.init(signingKey);
112-
return mac.doFinal(data);
96+
try {
97+
final Mac mac = Mac.getInstance(algorithm);
98+
mac.init(signingKey);
99+
return mac.doFinal(data);
113100

114-
} catch (final NoSuchAlgorithmException e) {
115-
throw new CryptoException(CryptoError.UNKNOWN_ALGORITH, e);
116-
} catch (final InvalidKeyException e) {
117-
throw new CryptoException(CryptoError.INVALID_CRYPTO_KEY, e);
118-
}
119-
}
101+
} catch (final NoSuchAlgorithmException e) {
102+
throw new CryptoException(CryptoError.UNKNOWN_ALGORITH, e);
103+
} catch (final InvalidKeyException e) {
104+
throw new CryptoException(CryptoError.INVALID_CRYPTO_KEY, e);
105+
}
106+
}
120107

121-
public static byte[] hmac(final InputStream data, final byte[] secretKey, String algorithm) throws CryptoException, IOException {
122-
final Map<String, String> javaStandardAlgorithmNames = ExpathCryptoModule.javaStandardAlgorithmNames;
108+
public static byte[] hmac(final InputStream data, final byte[] secretKey, String algorithm)
109+
throws CryptoException, IOException {
110+
final Map<String, String> javaStandardAlgorithmNames = ExpathCryptoModule.javaStandardAlgorithmNames;
123111

124-
if (javaStandardAlgorithmNames.containsKey(algorithm)) {
125-
algorithm = javaStandardAlgorithmNames.get(algorithm);
126-
}
112+
if (javaStandardAlgorithmNames.containsKey(algorithm)) {
113+
algorithm = javaStandardAlgorithmNames.get(algorithm);
114+
}
127115

128-
final SecretKeySpec signingKey = new SecretKeySpec(secretKey, algorithm);
116+
final SecretKeySpec signingKey = new SecretKeySpec(secretKey, algorithm);
129117

130-
try {
131-
final Mac mac = Mac.getInstance(algorithm);
132-
mac.init(signingKey);
118+
try {
119+
final Mac mac = Mac.getInstance(algorithm);
120+
mac.init(signingKey);
133121

134-
final byte[] buf = new byte[Buffer.TRANSFER_SIZE];
135-
int read = -1;
136-
while((read = data.read(buf)) > -1) {
137-
mac.update(buf, 0, read);
138-
}
122+
final byte[] buf = new byte[Buffer.TRANSFER_SIZE];
123+
int read = -1;
124+
while ((read = data.read(buf)) > -1) {
125+
mac.update(buf, 0, read);
126+
}
139127

140-
return mac.doFinal();
128+
return mac.doFinal();
141129

142-
} catch (final NoSuchAlgorithmException e) {
143-
throw new CryptoException(CryptoError.UNKNOWN_ALGORITH, e);
144-
} catch (final InvalidKeyException e) {
145-
throw new CryptoException(CryptoError.INVALID_CRYPTO_KEY, e);
146-
}
147-
}
130+
} catch (final NoSuchAlgorithmException e) {
131+
throw new CryptoException(CryptoError.UNKNOWN_ALGORITH, e);
132+
} catch (final InvalidKeyException e) {
133+
throw new CryptoException(CryptoError.INVALID_CRYPTO_KEY, e);
134+
}
135+
}
148136
}

0 commit comments

Comments
 (0)