HashUtil.java

1
package com.jsql.util.bruter;
2
3
import com.jsql.util.StringUtil;
4
5
import java.nio.charset.StandardCharsets;
6
import java.security.MessageDigest;
7
import java.security.NoSuchAlgorithmException;
8
import java.util.zip.CRC32;
9
import java.util.zip.Checksum;
10
11
public class HashUtil {
12
    
13
    private HashUtil() {
14
        // Nothing
15
    }
16
    
17
    public static String toAdler32(String text) {
18
19
        byte[] bytes = text.getBytes(StandardCharsets.UTF_8);
20
        Checksum checksum = new java.util.zip.Adler32();
21 1 1. toAdler32 : removed call to java/util/zip/Checksum::update → KILLED
        checksum.update(bytes,0,bytes.length);
22
        
23 1 1. toAdler32 : replaced return value with "" for com/jsql/util/bruter/HashUtil::toAdler32 → KILLED
        return String.valueOf(checksum.getValue());
24
    }
25
    
26
    public static String toCrc16(String text) {
27 1 1. toCrc16 : replaced return value with "" for com/jsql/util/bruter/HashUtil::toCrc16 → KILLED
        return Crc16Helper.generateCRC16(text);
28
    }
29
    
30
    public static String toCrc64(String text) {
31 1 1. toCrc64 : replaced return value with "" for com/jsql/util/bruter/HashUtil::toCrc64 → KILLED
        return Crc64Helper.generateCRC64(text.getBytes(StandardCharsets.UTF_8));
32
    }
33
    
34
    public static String toMySql(String textInput) throws NoSuchAlgorithmException {
35
        
36
        var md = MessageDigest.getInstance("sha-1");
37
        
38
        var password = String.valueOf(textInput.toCharArray());
39
        
40
        byte[] passwordBytes = password.getBytes(StandardCharsets.UTF_8);
41 1 1. toMySql : removed call to java/security/MessageDigest::update → SURVIVED
        md.update(passwordBytes, 0, passwordBytes.length);
42
        
43
        byte[] hashSHA1 = md.digest();
44
        var stringSHA1 = HashUtil.digestToHexString(hashSHA1);
45
        
46
        var passwordSHA1 = String.valueOf(StringUtil.hexstr(stringSHA1).toCharArray());
47
        byte[] passwordSHA1Bytes = passwordSHA1.getBytes(StandardCharsets.UTF_8);
48
        
49 1 1. toMySql : removed call to java/security/MessageDigest::update → SURVIVED
        md.update(passwordSHA1Bytes, 0, passwordSHA1Bytes.length);
50
        byte[] hashSHA1SH1 = md.digest();
51
        
52 1 1. toMySql : replaced return value with "" for com/jsql/util/bruter/HashUtil::toMySql → SURVIVED
        return HashUtil.digestToHexString(hashSHA1SH1);
53
    }
54
55
    public static String toCrc32(String textInput) {
56
        
57
        byte[] bytes = textInput.getBytes(StandardCharsets.UTF_8);
58
        
59
        Checksum checksum = new CRC32();
60 1 1. toCrc32 : removed call to java/util/zip/Checksum::update → KILLED
        checksum.update(bytes, 0, bytes.length);
61
        
62
        long lngChecksum = checksum.getValue();
63
        
64 1 1. toCrc32 : replaced return value with "" for com/jsql/util/bruter/HashUtil::toCrc32 → KILLED
        return Long.toString(lngChecksum);
65
    }
66
67
    public static String toMd4(String textInput) {
68
        
69
        MessageDigest md = new DigestMD4();
70
71
        var passwordString = String.valueOf(textInput.toCharArray());
72
        byte[] passwordByte = passwordString.getBytes(StandardCharsets.UTF_8);
73
        
74 1 1. toMd4 : removed call to java/security/MessageDigest::update → KILLED
        md.update(passwordByte, 0, passwordByte.length);
75
        byte[] encodedPassword = md.digest();
76
        
77 1 1. toMd4 : replaced return value with "" for com/jsql/util/bruter/HashUtil::toMd4 → KILLED
        return HashUtil.digestToHexString(encodedPassword);
78
    }
79
80
    public static String toHash(String nameMethod, String textInput) throws NoSuchAlgorithmException {
81
        
82
        var md = MessageDigest.getInstance(nameMethod);
83
        
84
        var passwordString = String.valueOf(textInput.toCharArray());
85
        byte[] passwordByte = passwordString.getBytes(StandardCharsets.UTF_8);
86
        
87 1 1. toHash : removed call to java/security/MessageDigest::update → TIMED_OUT
        md.update(passwordByte, 0, passwordByte.length);
88
        byte[] encodedPassword = md.digest();
89
        
90 1 1. toHash : replaced return value with "" for com/jsql/util/bruter/HashUtil::toHash → TIMED_OUT
        return HashUtil.digestToHexString(encodedPassword);
91
    }
92
    
93
    /**
94
     * Convert a digest hash to a string representation.
95
     * @param block Digest array
96
     * @return Hash as a string
97
     */
98
    public static String digestToHexString(byte[] block) {
99
        
100
        var buf = new StringBuilder();
101
102
        for (byte b : block) {
103 1 1. digestToHexString : removed call to com/jsql/util/bruter/HashUtil::byte2hex → TIMED_OUT
            HashUtil.byte2hex(b, buf);
104
        }
105
        
106 1 1. digestToHexString : replaced return value with "" for com/jsql/util/bruter/HashUtil::digestToHexString → TIMED_OUT
        return buf.toString();
107
    }
108
    
109
    /**
110
     * Convert byte character to hexadecimal StringBuffer character.
111
     * @param b Byte character to convert
112
     * @param buf Hexadecimal converted character
113
     */
114
    private static void byte2hex(byte b, StringBuilder buf) {
115
        
116
        var hexChars = new char[]{ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
117 2 1. byte2hex : Replaced Shift Right with Shift Left → KILLED
2. byte2hex : Replaced bitwise AND with OR → KILLED
        int high = (b & 0xf0) >> 4;
118 1 1. byte2hex : Replaced bitwise AND with OR → KILLED
        int low = b & 0x0f;
119
        buf.append(hexChars[high]);
120
        buf.append(hexChars[low]);
121
    }
122
}

Mutations

21

1.1
Location : toAdler32
Killed by : StringUtilSpock.[engine:spock]/[spec:StringUtilSpock]/[feature:$spock_feature_0_0]
removed call to java/util/zip/Checksum::update → KILLED

23

1.1
Location : toAdler32
Killed by : StringUtilSpock.[engine:spock]/[spec:StringUtilSpock]/[feature:$spock_feature_0_0]
replaced return value with "" for com/jsql/util/bruter/HashUtil::toAdler32 → KILLED

27

1.1
Location : toCrc16
Killed by : StringUtilSpock.[engine:spock]/[spec:StringUtilSpock]/[feature:$spock_feature_0_0]
replaced return value with "" for com/jsql/util/bruter/HashUtil::toCrc16 → KILLED

31

1.1
Location : toCrc64
Killed by : StringUtilSpock.[engine:spock]/[spec:StringUtilSpock]/[feature:$spock_feature_0_0]
replaced return value with "" for com/jsql/util/bruter/HashUtil::toCrc64 → KILLED

41

1.1
Location : toMySql
Killed by : none
removed call to java/security/MessageDigest::update → SURVIVED

49

1.1
Location : toMySql
Killed by : none
removed call to java/security/MessageDigest::update → SURVIVED

52

1.1
Location : toMySql
Killed by : none
replaced return value with "" for com/jsql/util/bruter/HashUtil::toMySql → SURVIVED

60

1.1
Location : toCrc32
Killed by : StringUtilSpock.[engine:spock]/[spec:StringUtilSpock]/[feature:$spock_feature_0_0]
removed call to java/util/zip/Checksum::update → KILLED

64

1.1
Location : toCrc32
Killed by : StringUtilSpock.[engine:spock]/[spec:StringUtilSpock]/[feature:$spock_feature_0_0]
replaced return value with "" for com/jsql/util/bruter/HashUtil::toCrc32 → KILLED

74

1.1
Location : toMd4
Killed by : StringUtilSpock.[engine:spock]/[spec:StringUtilSpock]/[feature:$spock_feature_0_0]
removed call to java/security/MessageDigest::update → KILLED

77

1.1
Location : toMd4
Killed by : StringUtilSpock.[engine:spock]/[spec:StringUtilSpock]/[feature:$spock_feature_0_0]
replaced return value with "" for com/jsql/util/bruter/HashUtil::toMd4 → KILLED

87

1.1
Location : toHash
Killed by : none
removed call to java/security/MessageDigest::update → TIMED_OUT

90

1.1
Location : toHash
Killed by : none
replaced return value with "" for com/jsql/util/bruter/HashUtil::toHash → TIMED_OUT

103

1.1
Location : digestToHexString
Killed by : none
removed call to com/jsql/util/bruter/HashUtil::byte2hex → TIMED_OUT

106

1.1
Location : digestToHexString
Killed by : none
replaced return value with "" for com/jsql/util/bruter/HashUtil::digestToHexString → TIMED_OUT

117

1.1
Location : byte2hex
Killed by : BruterSpock.[engine:spock]/[spec:BruterSpock]/[feature:$spock_feature_0_1]
Replaced Shift Right with Shift Left → KILLED

2.2
Location : byte2hex
Killed by : BruterSpock.[engine:spock]/[spec:BruterSpock]/[feature:$spock_feature_0_1]
Replaced bitwise AND with OR → KILLED

118

1.1
Location : byte2hex
Killed by : BruterSpock.[engine:spock]/[spec:BruterSpock]/[feature:$spock_feature_0_1]
Replaced bitwise AND with OR → KILLED

Active mutators

Tests examined


Report generated by PIT 1.16.1