Skip to content

Commit 032ecb8

Browse files
committed
added hex and base64 representation for hash result
1 parent 1adb73e commit 032ecb8

File tree

8 files changed

+133
-57
lines changed

8 files changed

+133
-57
lines changed

src/main/java/com/github/zrdj/java/primitives/bytes/Bytes.java

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,45 @@
66
import com.github.zrdj.java.primitives.hashing.HashingAlgorithm;
77

88
import java.io.ByteArrayInputStream;
9-
import java.nio.charset.StandardCharsets;
9+
import java.nio.charset.Charset;
1010
import java.util.Arrays;
1111

1212

1313
public interface Bytes {
1414
Bytes empty = new Bytes.Of();
1515

16+
final class Of extends AbstractBytes {
17+
18+
public Of(final String value, Charset charset) {
19+
this(value.getBytes(charset));
20+
}
21+
22+
public Of(final String value) {
23+
this(value.getBytes());
24+
}
25+
26+
public Of(final byte[] value) {
27+
super(value);
28+
}
29+
30+
public Of() {
31+
super(new byte[0]);
32+
}
33+
}
34+
1635
static Bytes of(final byte[] value) {
1736
return new Bytes.Of(value);
1837
}
38+
39+
static Bytes empty() {
40+
return empty;
41+
}
42+
43+
static Bytes of(String value, Charset charset) {
44+
return new Bytes.Of(value, charset);
45+
}
46+
47+
1948
byte[] bytes();
2049

2150
default long length() {
@@ -42,8 +71,8 @@ default Bytes decode(final Codec codec) {
4271
return Bytes.of(codec.decode(this.bytes()));
4372
}
4473

45-
default Bytes hash(final HashingAlgorithm algorithm) {
46-
return Bytes.of(algorithm.hash(this.bytes()));
74+
static Bytes of(String value) {
75+
return new Bytes.Of(value);
4776
}
4877

4978
default Bytes encryptAES(final Bytes key, final Bytes salt) {
@@ -96,19 +125,8 @@ public int hashCode() {
96125
}
97126
}
98127

99-
final class Of extends AbstractBytes {
100-
101-
public Of(final String value) {
102-
this(value.getBytes(StandardCharsets.UTF_8));
103-
}
104-
105-
public Of(final byte[] value) {
106-
super(value);
107-
}
108-
109-
public Of() {
110-
super(new byte[0]);
111-
}
128+
default Bytes hash(final HashingAlgorithm algorithm) {
129+
return Bytes.of(algorithm.hash(this.bytes()).value());
112130
}
113131

114132
}

src/main/java/com/github/zrdj/java/primitives/codec/Codec.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
public interface Codec {
44
byte[] encode(byte[] value);
55

6+
String encodeToString(byte[] value);
67
byte[] decode(byte[] value);
78
}

src/main/java/com/github/zrdj/java/primitives/codec/Codecs.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ public byte[] encode(final byte[] value) {
2020
return _codec.encode(value);
2121
}
2222

23+
@Override
24+
public String encodeToString(final byte[] value) {
25+
return _codec.encodeToString(value);
26+
}
27+
2328
@Override
2429
public byte[] decode(final byte[] value) {
2530
return _codec.decode(value);

src/main/java/com/github/zrdj/java/primitives/codec/base64/Base64BaseCodec.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ abstract class Base64BaseCodec implements Codec {
1313
_encoder = encoder;
1414
}
1515

16+
@Override
17+
public String encodeToString(final byte[] value) {
18+
return _encoder.encodeToString(value);
19+
}
20+
1621
@Override
1722
public final byte[] encode(final byte[] value) {
1823
return _encoder.encode(value);

src/main/java/com/github/zrdj/java/primitives/codec/hex/HexCodec.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ public byte[] encode(final byte[] value) {
2626
return result;
2727
}
2828

29+
@Override
30+
public String encodeToString(final byte[] value) {
31+
final StringBuilder builder = internalEncode(value);
32+
return builder.toString();
33+
}
34+
2935
@Override
3036
public byte[] decode(final byte[] value) {
3137
if (value.length % 2 != 0) {
Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,41 @@
11
package com.github.zrdj.java.primitives.hashing;
22

3-
import java.security.MessageDigest;
4-
import java.security.NoSuchAlgorithmException;
5-
6-
public enum Hash implements HashingAlgorithm {
7-
SHA("SHA"),
8-
SHA_224("SHA-224"),
9-
SHA_256("SHA-256"),
10-
SHA_384("SHA-384"),
11-
SHA_512("SHA-512"),
12-
SHA_512_224("SHA-512/224"),
13-
SHA_512_256("SHA-512/256"),
14-
SHA3_224("SHA3-224"),
15-
SHA3_256("SHA3-256"),
16-
SHA3_384("SHA3-384"),
17-
SHA3_512("SHA3-512"),
18-
MD2("MD2"),
19-
MD5("MD5"),
20-
;
21-
private final String _algorithm;
22-
private MessageDigest _digest = null;
23-
private NoSuchAlgorithmException _error = null;
24-
25-
Hash(final String algorithm) {
26-
_algorithm = algorithm;
27-
try {
28-
_digest = MessageDigest.getInstance(algorithm);
29-
} catch (NoSuchAlgorithmException e) {
30-
_error = e;
3+
import com.github.zrdj.java.primitives.codec.Codecs;
4+
5+
public interface Hash {
6+
final class Of implements Hash {
7+
private final byte[] _value;
8+
private final HashAlgorithms _algorithm;
9+
10+
public Of(final byte[] value, final HashAlgorithms algorithm) {
11+
_value = value;
12+
_algorithm = algorithm;
13+
}
14+
15+
public byte[] value() {
16+
return _value;
17+
}
18+
19+
@Override
20+
public HashingAlgorithm algorithm() {
21+
return _algorithm;
3122
}
3223
}
3324

34-
@Override
35-
public String algorithm() {
36-
return _algorithm;
25+
byte[] value();
26+
27+
HashingAlgorithm algorithm();
28+
29+
default String toHex() {
30+
return Codecs.Hex.encodeToString(value());
3731
}
3832

39-
@Override
40-
public boolean isAvailable() {
41-
return _error == null;
33+
default String toBase64() {
34+
return Codecs.Base64.encodeToString(value());
4235
}
4336

44-
@Override
45-
public byte[] hash(final byte[] input) {
46-
if (!isAvailable()) {
47-
throw new NoSuchHashAlgorithmException(_algorithm, _error);
48-
}
49-
return _digest.digest(input);
37+
default String toBase64Url() {
38+
return Codecs.Base64Url.encodeToString(value());
5039
}
40+
5141
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.github.zrdj.java.primitives.hashing;
2+
3+
import java.security.MessageDigest;
4+
import java.security.NoSuchAlgorithmException;
5+
6+
public enum HashAlgorithms implements HashingAlgorithm {
7+
SHA("SHA"),
8+
SHA_224("SHA-224"),
9+
SHA_256("SHA-256"),
10+
SHA_384("SHA-384"),
11+
SHA_512("SHA-512"),
12+
SHA_512_224("SHA-512/224"),
13+
SHA_512_256("SHA-512/256"),
14+
SHA3_224("SHA3-224"),
15+
SHA3_256("SHA3-256"),
16+
SHA3_384("SHA3-384"),
17+
SHA3_512("SHA3-512"),
18+
MD2("MD2"),
19+
MD5("MD5"),
20+
;
21+
private final String _algorithm;
22+
private MessageDigest _digest = null;
23+
private NoSuchAlgorithmException _error = null;
24+
25+
HashAlgorithms(final String algorithm) {
26+
_algorithm = algorithm;
27+
try {
28+
_digest = MessageDigest.getInstance(algorithm);
29+
} catch (NoSuchAlgorithmException e) {
30+
_error = e;
31+
}
32+
}
33+
34+
@Override
35+
public String algorithm() {
36+
return _algorithm;
37+
}
38+
39+
@Override
40+
public boolean isAvailable() {
41+
return _error == null;
42+
}
43+
44+
@Override
45+
public Hash hash(final byte[] input) {
46+
if (!isAvailable()) {
47+
throw new NoSuchHashAlgorithmException(_algorithm, _error);
48+
}
49+
return new Hash.Of(_digest.digest(input), this);
50+
}
51+
}

src/main/java/com/github/zrdj/java/primitives/hashing/HashingAlgorithm.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ public interface HashingAlgorithm {
55

66
boolean isAvailable();
77

8-
byte[] hash(byte[] input);
8+
Hash hash(byte[] input);
99
}

0 commit comments

Comments
 (0)