awk-gold-collection

My best software created for POSIX AWK
git clone git://git.luxferre.top/awk-gold-collection.git
Log | Files | Refs | Submodules | README

commit 5b97c119a45afa7091d2c6d1146bf8d9a83ed3f6
parent 2a982636690c414943dd8dfb759787d78499f08c
Author: Luxferre <lux@ferre>
Date:   Tue, 12 Mar 2024 10:52:09 +0200

Added cardgen.awk

Diffstat:
MREADME | 2++
Autils/cardgen.awk | 30++++++++++++++++++++++++++++++
2 files changed, 32 insertions(+), 0 deletions(-)

diff --git a/README b/README @@ -32,5 +32,7 @@ Other AWK scripts have the usage described in their comment headers. * utils/textereo.awk: ASCII art stereogram generator * utils/tgl.awk: The Great Library of useful functions missing in POSIX AWK * utils/imeigen.awk: Random valid IMEI generator (with optional TAC prefix) +* utils/cardgen.awk: Random valid banking card number generator + (with optional BIN prefix) --- Luxferre --- diff --git a/utils/cardgen.awk b/utils/cardgen.awk @@ -0,0 +1,30 @@ +# Valid banking card number generator script +# +# Usage: awk -f cardgen.awk [BIN] +# you can pass any amount of digits, if less than 16 then the rest +# is randomized and the checksum is calculated accordingly +# if more, then it's truncated to 15 and the checksum is calculated +# if no BIN is passed, a fully random valid card number is generated +# +# Created by Luxferre in 2024, released into public domain + +# Luhn checksum (picoLuhn variation for 15 digits) +function luhn(ccstr, acc, i, l, d) { + acc = 0 + l = length(ccstr) # must be odd in this variation + for(i=0;i<l;i++) { + d = int(substr(ccstr, i+1, 1)) # get current digit + acc = (acc + int(d * ((i%2) == 0 ? 2.2 : 1))) % 10 # accumulate + } + return (10 - acc) % 10 # finalize the check digit +} + +BEGIN { + srand() # init PRNG + cc = ARGV[1] + gsub(/[^0-9]/, "", cc) # remove all non-digits + for(i=0;i<15;i++) # append 15 random digits + cc = cc int(rand()*10) + cc = substr(cc, 1, 15) # truncate to first 15 digits + print(cc luhn(cc)) # output complete number with checksum +}