]> git.basschouten.com Git - openhab-addons.git/blob
346ae783fa1cedb9cd336097dd5046b0c3c561cd
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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 /**
18  * The {@link Checksum} is responsible for calculating checksum of given data.
19  *
20  * @author Boris Krivonog - Initial contribution
21  */
22 class Checksum {
23     static byte calculate(byte[]... lists) {
24         return Arrays.stream(lists).reduce((byte) 0, Checksum::calculate, (a, b) -> b);
25     }
26
27     static byte calculate(byte[] buffer, int offset, int count) {
28         return calculate((byte) 0, buffer, offset, count);
29     }
30
31     private static byte calculate(byte checksum, byte[] buffer) {
32         return calculate(checksum, buffer, 0, buffer.length);
33     }
34
35     private static byte calculate(byte checksum, byte[] buffer, int offset, int count) {
36         byte result = checksum;
37         int end = count + offset;
38         for (int index = offset; index < end; ++index) {
39             result = (byte) (result ^ buffer[index]);
40         }
41         return result;
42     }
43 }