-
Notifications
You must be signed in to change notification settings - Fork 0
/
CheckSumUtil.java
36 lines (30 loc) · 1.12 KB
/
CheckSumUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*
* Copyright (c) 2020 Gobierno de España
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* SPDX-License-Identifier: MPL-2.0
*/
package es.gob.radarcovid.verification.util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
@Slf4j
public class CheckSumUtil {
private CheckSumUtil() {}
public static int checkSum(String input) {
// Sample code - returns checkSum from input
int result = 0;
if (input != null && !StringUtils.isEmpty(input)) {
result = Character.getNumericValue(input.charAt(input.length() - 1));
}
return result;
}
public static boolean validateChecksum(String validationCode) {
// Sample code - returns if code is correct
int last = Character.getNumericValue(validationCode.charAt(validationCode.length() - 1));
int checksum = checkSum(validationCode.substring(0, validationCode.length() - 1));
return last == checksum;
}
}