rizin/librz/util/luhn.c
Giovanni 82e7917294
Refactoring Hash (#1082)
* Added authors and licenses and plugin access to crypto related files
* removed `rz_hash` and related files
* introduced `rz_msg_digest_*` with plugins
* updated all old methods with newer ones.
* added HMAC support to `rz-hash` (moved key option under `-K`)
* removed wrong algorithms on `woD`/`woE`
* fixed parity digest size
* fixed the behaviour on all hash functions
* added unit test for hash
* optimized code for HMAC key calculation
* removed hash legacy tests
2021-05-02 20:47:17 +08:00

32 lines
625 B
C

// SPDX-FileCopyrightText: 2021 deroad <wargio@libero.it>
// SPDX-License-Identifier: LGPL-3.0-only
#include <rz_util.h>
RZ_API bool rz_calculate_luhn_value(const char *data, ut64 *result) {
rz_return_val_if_fail(data && result, false);
ssize_t size = strlen(data);
if (size < 1) {
return false;
}
int digit;
ut64 sum = 0;
bool parity = false;
for (ssize_t i = size - 1; i >= 0; --i) {
if (!IS_DIGIT(data[i])) {
return false;
}
digit = data[i] - '0';
if (parity) {
digit *= 2;
}
digit = (digit / 10) + (digit % 10);
sum += digit;
parity = !parity;
}
*result = sum % 10;
return true;
}