]> git.basschouten.com Git - openhab-addons.git/blob
98b58c376a4db70d457bc29d71ce2aa7885b4bfa
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.regoheatpump.internal.rego6xx;
14
15 import java.util.Arrays;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18
19 /**
20  * The {@link Checksum} is responsible for calculating checksum of given data.
21  *
22  * @author Boris Krivonog - Initial contribution
23  */
24 @NonNullByDefault
25 class Checksum {
26     static byte calculate(byte[]... lists) {
27         return Arrays.stream(lists).reduce((byte) 0, Checksum::calculate, (a, b) -> b);
28     }
29
30     static byte calculate(byte[] buffer, int offset, int count) {
31         return calculate((byte) 0, buffer, offset, count);
32     }
33
34     private static byte calculate(byte checksum, byte[] buffer) {
35         return calculate(checksum, buffer, 0, buffer.length);
36     }
37
38     private static byte calculate(byte checksum, byte[] buffer, int offset, int count) {
39         byte result = checksum;
40         int end = count + offset;
41         for (int index = offset; index < end; ++index) {
42             result = (byte) (result ^ buffer[index]);
43         }
44         return result;
45     }
46 }