• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

trydofor / professional-mirana / #68

31 Aug 2024 02:56AM UTC coverage: 84.4% (+1.0%) from 83.382%
#68

push

web-flow
Merge pull request #45 from trydofor/develop

v2.7.3 with minor change

474 of 568 new or added lines in 27 files covered. (83.45%)

8 existing lines in 6 files now uncovered.

5237 of 6205 relevant lines covered (84.4%)

0.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

58.82
/src/main/java/pro/fessional/mirana/bits/MdHelp.java
1
package pro.fessional.mirana.bits;
2

3
import org.jetbrains.annotations.NotNull;
4
import org.jetbrains.annotations.Nullable;
5
import pro.fessional.mirana.data.Null;
6
import pro.fessional.mirana.io.InputStreams;
7

8
import java.io.InputStream;
9
import java.security.MessageDigest;
10
import java.security.NoSuchAlgorithmException;
11

12
import static java.nio.charset.StandardCharsets.UTF_8;
13

14
/**
15
 * MessageDigest helper and thread safe
16
 *
17
 * @author trydofor
18
 * @since 2021-01-24
19
 */
20
public class MdHelp {
21

22
    public final String algorithm;
23
    public final int hexLength;
24

UNCOV
25
    protected MdHelp(String algorithm) {
×
UNCOV
26
        this.algorithm = algorithm;
×
NEW
27
        this.hexLength = guessHexLength(algorithm);
×
NEW
28
    }
×
29

30
    protected MdHelp(String algorithm, int hexLen) {
1✔
31
        this.algorithm = algorithm;
1✔
32
        this.hexLength = hexLen;
1✔
33
    }
1✔
34

35
    public boolean isSum(@Nullable String str) {
NEW
36
        return Bytes.isHex(str, hexLength);
×
37
    }
38

39
    public boolean asSum(@Nullable String str) {
NEW
40
        return Bytes.asHex(str, hexLength);
×
41
    }
42

43
    @NotNull
44
    public String getAlgorithm() {
NEW
45
        return algorithm;
×
46
    }
47

48
    public int getHexLength() {
NEW
49
        return hexLength;
×
50
    }
51

52
    @NotNull
53
    public String sum(@Nullable String str) {
54
        return sum(str, true);
1✔
55
    }
56

57
    @NotNull
58
    public String sum(@Nullable InputStream ins) {
59
        return sum(ins, true);
1✔
60
    }
61

62
    @NotNull
63
    public String sum(byte @Nullable [] bytes) {
64
        return Bytes.hex(bytes, true);
×
65
    }
66

67
    @NotNull
68
    public String sum(@Nullable String str, boolean upper) {
69
        if (str == null) return Null.Str;
1✔
70
        return sum(str.getBytes(UTF_8), upper);
1✔
71
    }
72

73
    @NotNull
74
    public String sum(@Nullable InputStream ins, boolean upper) {
75
        if (ins == null) return Null.Str;
1✔
76
        byte[] bytes = InputStreams.readBytes(ins);
1✔
77
        return sum(bytes, upper);
1✔
78
    }
79

80
    @NotNull
81
    public String sum(byte @Nullable [] bytes, boolean upper) {
82
        if (bytes == null) return Null.Str;
1✔
83
        byte[] hash = digest(bytes);
1✔
84
        return Bytes.hex(hash, upper);
1✔
85
    }
86

87

88
    public byte @NotNull [] digest(byte @Nullable [] bytes) {
89
        if (bytes == null) return Null.Bytes;
1✔
90
        MessageDigest digest = newOne();
1✔
91
        digest.update(bytes);
1✔
92
        return digest.digest();
1✔
93
    }
94

95
    /**
96
     * create a Mac instance, the instance it not thread-safe
97
     */
98
    @NotNull
99
    public MessageDigest newOne() {
100
        return newOne(algorithm);
1✔
101
    }
102

103
    public boolean check(@Nullable String sum, byte @Nullable [] bytes) {
104
        if (bytes == null || sum == null) return false;
×
105
        String md5 = sum(bytes);
×
106
        return sum.equalsIgnoreCase(md5);
×
107
    }
108

109
    public boolean check(@Nullable String sum, @Nullable String str) {
110
        if (str == null || sum == null) return false;
1✔
111
        String md5 = sum(str);
1✔
112
        return sum.equalsIgnoreCase(md5);
1✔
113
    }
114

115
    public boolean check(@Nullable String sum, @Nullable InputStream ins) {
116
        if (ins == null || sum == null) return false;
1✔
117
        String md5 = sum(ins);
1✔
118
        return sum.equalsIgnoreCase(md5);
1✔
119
    }
120

121
    //
122
    public static final int LEN_MD5_HEX = 32;
123
    public static final int LEN_SHA1_HEX = 40;
124
    public static final int LEN_SHA256_HEX = 64;
125
    public static final String MD_MD5 = "MD5";
126
    public static final String MD_SHA1 = "SHA-1";
127
    public static final String MD_SHA256 = "SHA-256";
128

129
    public static final MdHelp md5 = of(MD_MD5, LEN_MD5_HEX);
1✔
130
    public static final MdHelp sha1 = of(MD_SHA1, LEN_SHA1_HEX);
1✔
131
    public static final MdHelp sha256 = of(MD_SHA256, LEN_SHA256_HEX);
1✔
132

133
    /**
134
     * guess hex sum length, return 0 if not (MD5,SHA-1,SHA-256)
135
     */
136
    public static int guessHexLength(@NotNull String algorithm) {
NEW
137
        if (MD_MD5.equalsIgnoreCase(algorithm)) return LEN_MD5_HEX;
×
NEW
138
        if (MD_SHA1.equalsIgnoreCase(algorithm)) return LEN_SHA1_HEX;
×
NEW
139
        if (MD_SHA256.equalsIgnoreCase(algorithm)) return LEN_SHA256_HEX;
×
NEW
140
        return 0;
×
141
    }
142

143
    @NotNull
144
    public static MdHelp of(@NotNull String algorithm) {
UNCOV
145
        return new MdHelp(algorithm);
×
146
    }
147

148
    @NotNull
149
    public static MdHelp of(@NotNull String algorithm, int hexLen) {
150
        return new MdHelp(algorithm, hexLen);
1✔
151
    }
152

153
    @NotNull
154
    public static MessageDigest newMd5() {
NEW
155
        return newOne(MD_MD5);
×
156
    }
157

158
    @NotNull
159
    public static MessageDigest newSha1() {
NEW
160
        return newOne(MD_SHA1);
×
161
    }
162

163
    @NotNull
164
    public static MessageDigest newSha256() {
NEW
165
        return newOne(MD_SHA256);
×
166
    }
167

168
    @NotNull
169
    public static MessageDigest newOne(@NotNull String algorithm) {
170
        try {
171
            return MessageDigest.getInstance(algorithm);
1✔
172
        }
173
        catch (NoSuchAlgorithmException e) {
×
174
            throw new IllegalStateException("can not init algorithm=" + algorithm, e);
×
175
        }
176
    }
177

178
    /**
179
     * Auto check sum based on sum length, and obj type
180
     *
181
     * @param sum case-insensitive HEX string, support MD5,SHA1,SHA256
182
     * @param obj support byte[], CharSequence, InputStream
183
     */
184
    public static boolean checks(@Nullable String sum, @Nullable Object obj) {
185
        if (obj == null || sum == null) return false;
1✔
186

187
        final int len = sum.length();
1✔
188
        final MdHelp help;
189
        if (len == LEN_MD5_HEX) {
1✔
190
            help = md5;
1✔
191
        }
192
        else if (len == LEN_SHA1_HEX) {
1✔
193
            help = sha1;
1✔
194
        }
195
        else if (len == LEN_SHA256_HEX) {
1✔
196
            help = sha256;
1✔
197
        }
198
        else {
199
            return false;
×
200
        }
201

202
        if (obj instanceof CharSequence) {
1✔
203
            return help.check(sum, obj.toString());
1✔
204
        }
205

206
        if (obj instanceof byte[]) {
×
207
            return help.check(sum, (byte[]) obj);
×
208
        }
209

210
        if (obj instanceof InputStream) {
×
211
            return help.check(sum, (InputStream) obj);
×
212
        }
213

214
        return false;
×
215
    }
216
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc