fr.arakne.utils.encoding.Checksum   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 63
ccs 7
cts 7
cp 1
rs 10
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A verify(String,String) 0 3 1
A verify(String,int) 0 3 1
A Checksum() 0 1 1
A integer(String) 0 10 2
A hexadecimal(String) 0 3 1
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
import org.checkerframework.checker.index.qual.NonNegative;
23
import org.checkerframework.common.value.qual.IntRange;
24
import org.checkerframework.dataflow.qual.Pure;
25
import org.checkerframework.dataflow.qual.SideEffectFree;
26
27
/**
28
 * Implementation of the Dofus network checksum
29
 *
30
 * https://github.com/Emudofus/Dofus/blob/1.29/dofus/aks/Aks.as#L248
31
 */
32
public final class Checksum {
33
    /**
34
     * Utility class : disable constructor
35
     */
36
    private Checksum() {}
37
38
    /**
39
     * Compute the checksum as integer
40
     * The returned value is in interval [0-16]
41
     *
42
     * @param value Value to compute
43
     *
44
     * @return The checksum of value
45
     */
46
    @Pure
47
    @SuppressWarnings("compound.assignment") // chatAt cannot be negative
48
    public static @IntRange(from = 0, to = 15) int integer(String value) {
49 1
        @NonNegative int checksum = 0;
50
51 1
        for (int i = 0; i < value.length(); ++i) {
52 1
            checksum += value.charAt(i) % 16;
53
        }
54
55 1
        return checksum % 16;
56
    }
57
58
    /**
59
     * Compute the checksum as hexadecimal string
60
     * The return value is a single hexadecimal char string
61
     *
62
     * @param value Value to compute
63
     *
64
     * @return The checksum of value
65
     */
66
    @SideEffectFree
67
    public static String hexadecimal(String value) {
68 1
        return Integer.toHexString(integer(value)).toUpperCase();
69
    }
70
71
    /**
72
     * Verify the checksum of the input value
73
     *
74
     * @param input The input value to check
75
     * @param expectedChecksum The expected checksum as int
76
     *
77
     * @return true if checksum match
78
     */
79
    @Pure
80
    public static boolean verify(String input, int expectedChecksum) {
81 1
        return integer(input) == expectedChecksum;
82
    }
83
84
    /**
85
     * Verify the checksum of the input value
86
     *
87
     * @param input The input value to check
88
     * @param expectedChecksum The expected checksum as hexadecimal string
89
     *
90
     * @return true if checksum match
91
     */
92
    @Pure
93
    public static boolean verify(String input, String expectedChecksum) {
94 1
        return integer(input) == Integer.parseInt(expectedChecksum, 16);
95
    }
96
}
97