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

cardgen.awk (1045B)


      1 # Valid banking card number generator script
      2 #
      3 # Usage: awk -f cardgen.awk [BIN]
      4 # you can pass any amount of digits, if less than 16 then the rest
      5 # is randomized and the checksum is calculated accordingly
      6 # if more, then it's truncated to 15 and the checksum is calculated 
      7 # if no BIN is passed, a fully random valid card number is generated
      8 #
      9 # Created by Luxferre in 2024, released into public domain
     10 
     11 # Luhn checksum (picoLuhn variation for 15 digits)
     12 function luhn(ccstr,  acc, i, l, d) {
     13   acc = 0
     14   l = length(ccstr) # must be odd in this variation
     15   for(i=0;i<l;i++) {
     16     d = int(substr(ccstr, i+1, 1)) # get current digit
     17     acc = (acc + int(d * ((i%2) == 0 ? 2.2 : 1))) % 10 # accumulate
     18   }
     19   return (10 - acc) % 10 # finalize the check digit
     20 }
     21 
     22 BEGIN {
     23   srand() # init PRNG
     24   cc = ARGV[1]
     25   gsub(/[^0-9]/, "", cc) # remove all non-digits
     26   for(i=0;i<15;i++) # append 15 random digits
     27     cc = cc int(rand()*10)
     28   cc = substr(cc, 1, 15) # truncate to first 15 digits
     29   print(cc luhn(cc)) # output complete number with checksum
     30 }