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 defines objects for Verhoeff's check-digit algorithm
22 * for strings with various bases and an object for the core
23 * algorithm operations.
32 #include <support/DLLUtil.h>
36 // Verhoeff10 -- Implements Verhoeff's check-digit algorithm for base-10 strings.
38 class DLL_EXPORT Verhoeff10
40 friend class VerhoeffTest;
49 // Compute a check character for a given string.
50 static char ComputeCheckChar(const char * str);
51 static char ComputeCheckChar(const char * str, size_t strLen);
53 // Verify a check character against a given string.
54 static bool ValidateCheckChar(char checkChar, const char * str);
55 static bool ValidateCheckChar(char checkChar, const char * str, size_t strLen);
57 // Verify a check character at the end of a given string.
58 static bool ValidateCheckChar(const char * str);
59 static bool ValidateCheckChar(const char * str, size_t strLen);
61 // Convert between a character and its corresponding value.
62 static int CharToVal(char ch);
63 static char ValToChar(int val);
66 Verhoeff10() = delete;
67 ~Verhoeff10() = delete;
69 static uint8_t sMultiplyTable[];
70 static uint8_t sPermTable[];
73 // Verhoeff16 -- Implements Verhoeff's check-digit algorithm for base-16 (hex) strings.
75 class DLL_EXPORT Verhoeff16
77 friend class VerhoeffTest;
86 // Compute a check character for a given string.
87 static char ComputeCheckChar(const char * str);
88 static char ComputeCheckChar(const char * str, size_t strLen);
90 // Verify a check character against a given string.
91 static bool ValidateCheckChar(char checkChar, const char * str);
92 static bool ValidateCheckChar(char checkChar, const char * str, size_t strLen);
94 // Verify a check character at the end of a given string.
95 static bool ValidateCheckChar(const char * str);
96 static bool ValidateCheckChar(const char * str, size_t strLen);
98 // Convert between a character and its corresponding value.
99 static int CharToVal(char ch);
100 static char ValToChar(int val);
103 Verhoeff16() = delete;
104 ~Verhoeff16() = delete;
106 static uint8_t sMultiplyTable[];
107 static uint8_t sPermTable[];
110 // Verhoeff32 -- Implements Verhoeff's check-digit algorithm for base-32 strings.
112 // Character Set (any case): 0-9, A-H, J-N, P, R-Y (excludes I, O, Q and Z).
114 class DLL_EXPORT Verhoeff32
116 friend class VerhoeffTest;
125 // Compute a check character for a given string.
126 static char ComputeCheckChar(const char * str);
127 static char ComputeCheckChar(const char * str, size_t strLen);
129 // Verify a check character against a given string.
130 static bool ValidateCheckChar(char checkChar, const char * str);
131 static bool ValidateCheckChar(char checkChar, const char * str, size_t strLen);
133 // Verify a check character at the end of a given string.
134 static bool ValidateCheckChar(const char * str);
135 static bool ValidateCheckChar(const char * str, size_t strLen);
137 // Convert between a character and its corresponding value.
138 static int CharToVal(char ch);
139 static char ValToChar(int val);
142 Verhoeff32() = delete;
143 ~Verhoeff32() = delete;
145 static uint8_t sMultiplyTable[];
146 static uint8_t sPermTable[];
147 static int8_t sCharToValTable[];
148 static char sValToCharTable[];
151 // Verhoeff36 -- Implements Verhoeff's check-digit algorithm for base-36 strings.
153 // Character Set (any case) : 0-9, A-Z.
155 class DLL_EXPORT Verhoeff36
157 friend class VerhoeffTest;
166 static char ComputeCheckChar(const char * str);
167 static char ComputeCheckChar(const char * str, size_t strLen);
168 static bool ValidateCheckChar(char checkChar, const char * str);
169 static bool ValidateCheckChar(char checkChar, const char * str, size_t strLen);
170 static bool ValidateCheckChar(const char * str);
171 static bool ValidateCheckChar(const char * str, size_t strLen);
173 static int CharToVal(char ch);
174 static char ValToChar(int val);
177 Verhoeff36() = delete;
178 ~Verhoeff36() = delete;
180 static uint8_t sMultiplyTable[];
181 static uint8_t sPermTable[];
182 static int8_t sCharToValTable[];
183 static char sValToCharTable[];
186 // Verhoeff -- Implements core functions for Verhoeff's algorithm.
191 static int DihedralMultiply(int x, int y, int n);
192 static int DihedralInvert(int val, int n);
193 static int Permute(int val, uint8_t * permTable, int permTableLen, uint64_t iterCount);