3 * Copyright (c) 2020 Project CHIP Authors
4 * Copyright (c) 2013-2017 Nest Labs, Inc.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
21 * This file implements an object for Verhoeff's check-digit
22 * algorithm for base-16 strings.
30 #ifndef VERHOEFF16_NO_MULTIPLY_TABLE
32 uint8_t Verhoeff16::sMultiplyTable[] = {
33 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 0, 9, 10, 11, 12, 13, 14, 15, 8,
34 2, 3, 4, 5, 6, 7, 0, 1, 10, 11, 12, 13, 14, 15, 8, 9, 3, 4, 5, 6, 7, 0, 1, 2, 11, 12, 13, 14, 15, 8, 9, 10,
35 4, 5, 6, 7, 0, 1, 2, 3, 12, 13, 14, 15, 8, 9, 10, 11, 5, 6, 7, 0, 1, 2, 3, 4, 13, 14, 15, 8, 9, 10, 11, 12,
36 6, 7, 0, 1, 2, 3, 4, 5, 14, 15, 8, 9, 10, 11, 12, 13, 7, 0, 1, 2, 3, 4, 5, 6, 15, 8, 9, 10, 11, 12, 13, 14,
37 8, 15, 14, 13, 12, 11, 10, 9, 0, 7, 6, 5, 4, 3, 2, 1, 9, 8, 15, 14, 13, 12, 11, 10, 1, 0, 7, 6, 5, 4, 3, 2,
38 10, 9, 8, 15, 14, 13, 12, 11, 2, 1, 0, 7, 6, 5, 4, 3, 11, 10, 9, 8, 15, 14, 13, 12, 3, 2, 1, 0, 7, 6, 5, 4,
39 12, 11, 10, 9, 8, 15, 14, 13, 4, 3, 2, 1, 0, 7, 6, 5, 13, 12, 11, 10, 9, 8, 15, 14, 5, 4, 3, 2, 1, 0, 7, 6,
40 14, 13, 12, 11, 10, 9, 8, 15, 6, 5, 4, 3, 2, 1, 0, 7, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,
43 #endif // VERHOEFF16_NO_MULTIPLY_TABLE
45 uint8_t Verhoeff16::sPermTable[] = { 4, 7, 5, 14, 8, 12, 15, 0, 2, 11, 3, 13, 10, 6, 9, 1 };
47 char Verhoeff16::ComputeCheckChar(const char * str)
49 return ComputeCheckChar(str, strlen(str));
52 char Verhoeff16::ComputeCheckChar(const char * str, size_t strLen)
56 for (size_t i = 1; i <= strLen; i++)
58 char ch = str[strLen - i];
60 int val = CharToVal(ch);
62 return 0; // invalid character
64 int p = Verhoeff::Permute(val, sPermTable, Base, i);
66 #ifdef VERHOEFF16_NO_MULTIPLY_TABLE
67 c = Verhoeff::DihedralMultiply(c, p, PolygonSize);
69 c = sMultiplyTable[c * Base + p];
73 c = Verhoeff::DihedralInvert(c, PolygonSize);
78 bool Verhoeff16::ValidateCheckChar(char checkChar, const char * str)
80 return ValidateCheckChar(checkChar, str, strlen(str));
83 bool Verhoeff16::ValidateCheckChar(char checkChar, const char * str, size_t strLen)
85 return (ComputeCheckChar(str, strLen) == checkChar);
88 bool Verhoeff16::ValidateCheckChar(const char * str)
90 return ValidateCheckChar(str, strlen(str));
93 bool Verhoeff16::ValidateCheckChar(const char * str, size_t strLen)
97 return ValidateCheckChar(str[strLen - 1], str, strLen - 1);
100 int Verhoeff16::CharToVal(char ch)
102 if (ch >= '0' && ch <= '9')
105 if (ch >= 'A' && ch <= 'F')
106 return (ch - 'A') + 10;
108 if (ch >= 'a' && ch <= 'f')
109 return (ch - 'a') + 10;
114 char Verhoeff16::ValToChar(int val)
116 if (val >= 0 && val < 10)
117 return static_cast<char>('0' + val);
119 if (val >= 10 && val < Base)
120 return static_cast<char>('A' + (val - 10));