0.1.0 LUHN

This commit is contained in:
rob tillaart 2022-12-29 16:15:53 +01:00
parent cc70071cc3
commit 179c0f62bb
16 changed files with 523 additions and 0 deletions

View File

@ -0,0 +1,27 @@
platforms:
rpipico:
board: rp2040:rp2040:rpipico
package: rp2040:rp2040
gcc:
features:
defines:
- ARDUINO_ARCH_RP2040
warnings:
flags:
packages:
rp2040:rp2040:
url: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json
compile:
# Choosing to run compilation tests on 2 different Arduino platforms
platforms:
- uno
# - due
# - zero
# - leonardo
- m4
- esp32
# - esp8266
# - mega2560
- rpipico

4
libraries/LUHN/.github/FUNDING.yml vendored Normal file
View File

@ -0,0 +1,4 @@
# These are supported funding model platforms
github: RobTillaart

View File

@ -0,0 +1,13 @@
name: Arduino-lint
on: [push, pull_request]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: arduino/arduino-lint-action@v1
with:
library-manager: update
compliance: strict

View File

@ -0,0 +1,17 @@
---
name: Arduino CI
on: [push, pull_request]
jobs:
runTest:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: ruby/setup-ruby@v1
with:
ruby-version: 2.6
- run: |
gem install arduino_ci
arduino_ci.rb

View File

@ -0,0 +1,18 @@
name: JSON check
on:
push:
paths:
- '**.json'
pull_request:
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: json-syntax-check
uses: limitusus/json-syntax-check@v1
with:
pattern: "\\.json$"

View File

@ -0,0 +1,11 @@
# Change Log LUHN
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
## [0.1.0] -2022-12-24
- initial version

21
libraries/LUHN/LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2022-2022 Rob Tillaart
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

103
libraries/LUHN/LUHN.cpp Normal file
View File

@ -0,0 +1,103 @@
//
// FILE: LUHN.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// DATE: 2022-12-24
// PURPOSE: Arduino Library for calculating LUHN checksum.
// URL: https://github.com/RobTillaart/LUHN
#include "LUHN.h"
LUHN::LUHN()
{
}
bool LUHN::isValid(const char * buffer)
{
return isValid((char*)buffer);
}
bool LUHN::isValid(char * buffer)
{
uint16_t checksum = 0;
uint8_t length = strlen(buffer);
if (length == 0) return false;
for (int i = 0; i < length-1; i++)
{
uint8_t x = buffer[i] - '0';
if (i % 2 == 0) checksum += x; // weight 1
else if (x < 5) checksum += x * 2; // weight 2
else checksum += (x * 2 - 10 + 1); // weight 2 + handle overflow.
}
return ((10000 - checksum) % 10) == (buffer[length-1] - '0');
}
char LUHN::generateChecksum(char * buffer)
{
uint16_t checksum = 0;
uint8_t length = strlen(buffer);
for (int i = 0; i < length; i++)
{
uint8_t x = buffer[i] - '0';
if (i % 2 == 0) checksum += x; // weight 1
else if (x < 5) checksum += x * 2; // weight 2
else checksum += (x * 2 - 10 + 1); // weight 2 + handle overflow.
}
return '0' + ((10000 - checksum) % 10);
}
void LUHN::randomize(uint32_t a, uint32_t b)
{
m_z = a;
m_w = b;
}
bool LUHN::generate(char * buffer, uint8_t length, char * prefix)
{
int len = strlen(prefix);
if (len >= length) return false;
if (length < 2) return false;
strcpy(buffer, prefix);
int i;
for (i = len; i < length-1;)
{
buffer[i++] = '0' + Marsaglia_mod10();
buffer[i] = '\0';
}
char c = generateChecksum(buffer);
buffer[i++] = c;
buffer[i] = '\0';
return true;
}
///////////////////////////////////////////////////////////////////////////
//
// PROTECTED
//
///////////////////////////////////////////////////////////////////////////
//
// An example of a simple pseudo-random number generator is the
// Multiply-with-carry method invented by George Marsaglia.
// it has two initializers (not zero) which can be changed
// to seed the generator.
// this is derived work.
//
uint8_t LUHN::Marsaglia_mod10()
{
m_z = 36969L * (m_z & 65535L) + (m_z >> 16);
m_w = 18000L * (m_w & 65535L) + (m_w >> 16);
return (m_z ^ m_w) % 10; // changed
}
// -- END OF FILE --

39
libraries/LUHN/LUHN.h Normal file
View File

@ -0,0 +1,39 @@
#pragma once
//
// FILE: LUHN.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// DATE: 2022-12-24
// PURPOSE: Arduino Library for calculating LUHN checksum.
// URL: https://github.com/RobTillaart/LUHN
#include "Arduino.h"
#define LUHN_LIB_VERSION (F("0.1.0"))
class LUHN
{
public:
LUHN();
// buffer == \0 terminated
bool isValid(const char * buffer);
bool isValid(char * buffer);
char generateChecksum(char * buffer);
// GENERATING AN ID
void randomize(uint32_t a, uint32_t b);
bool generate(char * buffer, uint8_t length, char * prefix);
protected:
uint32_t m_w = 1; // random generator parameter
uint32_t m_z = 2; // random generator parameter
uint8_t Marsaglia_mod10();
};
// -- END OF FILE --

71
libraries/LUHN/README.md Normal file
View File

@ -0,0 +1,71 @@
[![Arduino CI](https://github.com/RobTillaart/LUHN/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
[![Arduino-lint](https://github.com/RobTillaart/LUHN/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/LUHN/actions/workflows/arduino-lint.yml)
[![JSON check](https://github.com/RobTillaart/LUHN/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/LUHN/actions/workflows/jsoncheck.yml)
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/LUHN/blob/master/LICENSE)
[![GitHub release](https://img.shields.io/github/release/RobTillaart/LUHN.svg?maxAge=3600)](https://github.com/RobTillaart/LUHN/releases)
# LUHN
Arduino Library for calculating LUHN checksum.
## Description
The LUHN algorithm is a checksum (a.k.a. mod 10) invented by **Hans Peter Luhn** in 1960.
It is used to validate a variety of product ID's to detect typing errors an/of digit swaps.
So it is not to secure a number but to prevent common mistakes when entering a code in a system.
The LUHN check uses very few resources and is pretty fast.
Basic idea is to put all digits-1 through the formula and the output should equal the last digit.
Note: some LUHN validations uses the reversed product string.
#### related
- https://en.wikipedia.org/wiki/Luhn_algorithm
- https://github.com/RobTillaart/Adler
- https://github.com/RobTillaart/CRC
## Interface
```cpp
#include "LUHN.h"
```
- **LUHN()** constructor
- **bool isValid(char \* buffer )** validates the code in the parameter buffer.
The parameter buffer is a '\0' terminated char array. Length should be less than 254.
- **bool isValid(const char \* buffer )** idem.
- **char generateChecksum(char \* buffer)**
Returns the char '0'..'9' which is the checksum of the code in the parameter buffer.
The parameter buffer is a '\0' terminated char array. Length should be less than 254.
- **bool generate(char \* buffer, uint8_t length, char \* prefix)**
Generates a char array including LUHN number with a defined prefix of max length.
Returns false if the prefix exceeds length -1.
## Future
#### must
- update documentation
#### should
- unit tests
- look for optimization
- stream interface
- **uint8_t add(char c)** add char, return luhn sofar
- **uint8_t reset()** reset luhn, return last luhn or void?
#### could
- uint32_t interface for up to 8 digit ID's (99.999.999)
- **isValid(uint32_t)**
- **generateChecksum(uint32_t)**
- how about leading zero's

View File

@ -0,0 +1,51 @@
// FILE: luhn_check.ino
// AUTHOR: Rob Tillaart
// DATE: 2022-12-24
// PURPOSE: demo
// URL: https://github.com/RobTillaart/LUHN
#include "Arduino.h"
#include "LUHN.h"
uint32_t start, stop;
LUHN checker;
// example https://en.wikipedia.org/wiki/Luhn_algorithm
char cardNumber[20] = "79927398713";
void setup()
{
Serial.begin(115200);
while (!Serial);
Serial.println();
Serial.println(__FILE__);
// SHOULD PRINT 3
Serial.println(checker.generateChecksum((char *)"7992739871"));
}
void loop()
{
char buffer[50] = "";
int len = 0;
Serial.println("give string");
while (buffer[len - 1] != '\n')
{
if (Serial.available())
{
buffer[len++] = Serial.read();
buffer[len] = '\0';
}
}
Serial.print(buffer);
Serial.print(" ==> ");
Serial.println(checker.generateChecksum(buffer));
}
// -- END OF FILE --

View File

@ -0,0 +1,45 @@
// FILE: luhn_generate.ino
// AUTHOR: Rob Tillaart
// DATE: 2022-12-24
// PURPOSE: demo
// URL: https://github.com/RobTillaart/LUHN
#include "Arduino.h"
#include "LUHN.h"
uint32_t start, stop;
LUHN checker;
char number[50];
void setup()
{
Serial.begin(115200);
while (!Serial);
Serial.println();
Serial.println(__FILE__);
}
void loop()
{
start = micros();
bool b = checker.generate(number, 26, (char *)"1234");
stop = micros();
Serial.print(b);
Serial.print("\t");
Serial.print(checker.isValid(number));
Serial.print("\t");
Serial.print(stop - start);
// Serial.print((1.0*(stop - start))/ strlen(number)); per digit
Serial.print("\t");
Serial.println(number);
delay(100);
}
// -- END OF FILE --

View File

@ -0,0 +1,52 @@
// FILE: luhn_isValid.ino
// AUTHOR: Rob Tillaart
// DATE: 2022-12-24
// PURPOSE: demo
// URL: https://github.com/RobTillaart/LUHN
#include "Arduino.h"
#include "LUHN.h"
uint32_t start, stop;
LUHN checker;
void setup()
{
Serial.begin(115200);
while (!Serial);
Serial.println();
Serial.println(__FILE__);
// example https://en.wikipedia.org/wiki/Luhn_algorithm
test("79927398713");
test("49927398716");
test("49927398717"); // false
test("1234567812345671");
test("1234567812345670"); // false
test("371449635398431");
}
void loop()
{
}
void test(char * nr)
{
delay(10);
start = micros();
bool b = checker.isValid(nr);
stop = micros();
Serial.print("VALID:\t");
Serial.println(b);
Serial.print("TIME:\t");
Serial.println(stop - start);
Serial.println();
}
// -- END OF FILE --

View File

@ -0,0 +1,17 @@
# Syntax Colouring Map For LUHN
# Data types (KEYWORD1)
LUHN KEYWORD1
# Methods and Functions (KEYWORD2)
isValid KEYWORD2
generateChecksum KEYWORD2
randomize KEYWORD2
generate KEYWORD2
# Constants (LITERAL1)
LUHN_LIB_VERSION LITERAL1

View File

@ -0,0 +1,23 @@
{
"name": "LUHN",
"keywords": "checksum,validation",
"description": "Arduino Library for calculating LUHN checksum.",
"authors":
[
{
"name": "Rob Tillaart",
"email": "Rob.Tillaart@gmail.com",
"maintainer": true
}
],
"repository":
{
"type": "git",
"url": "https://github.com/RobTillaart/LUHN.git"
},
"version": "0.1.0",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*",
"headers": "LUHN.h"
}

View File

@ -0,0 +1,11 @@
name=LUHN
version=0.1.0
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino Library for calculating LUHN checksum.
paragraph=
category=Data Processing
url=https://github.com/RobTillaart/LUHN
architectures=*
includes=LUHN.h
depends=