1
|
|
|
/* |
2
|
|
|
* This file is part of ArakneUtils. |
3
|
|
|
* |
4
|
|
|
* ArakneUtils is free software: you can redistribute it and/or modify |
5
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
6
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
7
|
|
|
* (at your option) any later version. |
8
|
|
|
* |
9
|
|
|
* ArakneUtils is distributed in the hope that it will be useful, |
10
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
11
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12
|
|
|
* GNU Lesser General Public License for more details. |
13
|
|
|
* |
14
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
15
|
|
|
* along with ArakneUtils. If not, see <https://www.gnu.org/licenses/>. |
16
|
|
|
* |
17
|
|
|
* Copyright (c) 2017-2020 Vincent Quatrevieux |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
package fr.arakne.utils.encoding; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Implementation of the Dofus network checksum |
24
|
|
|
* |
25
|
|
|
* https://github.com/Emudofus/Dofus/blob/1.29/dofus/aks/Aks.as#L248 |
26
|
|
|
*/ |
27
|
|
|
final public class Checksum { |
28
|
|
|
/** |
29
|
|
|
* Utility class : disable constructor |
30
|
|
|
*/ |
31
|
|
|
private Checksum() {} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Compute the checksum as integer |
35
|
|
|
* The returned value is in interval [0-16] |
36
|
|
|
* |
37
|
|
|
* @param value Value to compute |
38
|
|
|
* |
39
|
|
|
* @return The checksum of value |
40
|
|
|
*/ |
41
|
|
|
static public int integer(String value) { |
42
|
1 |
|
int checksum = 0; |
43
|
|
|
|
44
|
1 |
|
for (int i = 0; i < value.length(); ++i) { |
45
|
1 |
|
checksum += value.charAt(i) % 16; |
46
|
|
|
} |
47
|
|
|
|
48
|
1 |
|
return checksum % 16; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Compute the checksum as hexadecimal string |
53
|
|
|
* The return value is a single hexadecimal char string |
54
|
|
|
* |
55
|
|
|
* @param value Value to compute |
56
|
|
|
* |
57
|
|
|
* @return The checksum of value |
58
|
|
|
*/ |
59
|
|
|
static public String hexadecimal(String value) { |
60
|
1 |
|
return Integer.toHexString(integer(value)).toUpperCase(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Verify the checksum of the input value |
65
|
|
|
* |
66
|
|
|
* @param input The input value to check |
67
|
|
|
* @param expectedChecksum The expected checksum as int |
68
|
|
|
* |
69
|
|
|
* @return true if checksum match |
70
|
|
|
*/ |
71
|
|
|
static public boolean verify(String input, int expectedChecksum) { |
72
|
1 |
|
return integer(input) == expectedChecksum; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Verify the checksum of the input value |
77
|
|
|
* |
78
|
|
|
* @param input The input value to check |
79
|
|
|
* @param expectedChecksum The expected checksum as hexadecimal string |
80
|
|
|
* |
81
|
|
|
* @return true if checksum match |
82
|
|
|
*/ |
83
|
|
|
static public boolean verify(String input, String expectedChecksum) { |
84
|
1 |
|
return integer(input) == Integer.parseInt(expectedChecksum, 16); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|