TL;DR → The main problem is coming up with a way to reorder an array non-randomly but without introducing bulky code. Like the effect of shuffling a deck of cards in a deterministic cheating way.


Full background:

I would like to generate reference numbers for letters sent via postal mail. An sqlite db is used to track the sequence numbers (but not the reference numbers). This is the bash code I have so far:

typeset -a symbolset=(a b c d e f g h   j k   m n   p q r s t u v w x y z     2 3 4 5 6 7 8 9)
ln_symbolset=${#symbolset[@]}; # 41 is the answer, not 42
itemseq=$(sqlite3 ltr_tracking.db "select max(counter) from $tbl;")
printf '%s\n' "next letter reference number is: $(date +%Y)-${symbolset[$((itemseq / ln_symbolset))]}${symbolset[$((itemseq % ln_symbolset))]}"

An array is defined with alphanumeric symbols, taking care to eliminate symbols that humans struggle to distinguish (e.g. 1l0o). Then integer div and mod operations produce a two character number which is then prefixed with the year. So e.g. 2024-aa. Just two chars gives more numbers than would ever be generated in one calandar year.

This code mostly satisfies the need. But there’s a problem: a recipient who receives two letters can easily realise how many letters were sent in the time span of the two letters they receive. Most numbers will start with “a” “b” or “c”.

I do not need or want a cryptographic level of security which then leads to ungodly 16 byte numbers. Simplicity¹ is far more important than confidentiality. Just a small tweak to stifle the most trivial analysis would be useful.

One temptation is to simply manually mix up the order of chars in the symbolset array, hard-coded. But then that makes the code less readible. So I probably need to create a 2nd array “symbolseq” which arbitrarily unorders the symbolset array. I say arbitrary and not random because the sequence must be deterministic and static from one execution to the next.

An associative array is one idea:

typeset -A symbolset_lookup_table=(
[a]=k
[b]=3
[c]=s
…

I’m just slightly put off by the fact that it’s not readily evident that the RHS values are all used from the same set as the LHS keys exactly once.

I should probably encode the year as well. This would give a two char year:

printf '%s ' "$(((2024/41) % 41))" "$((2024 % 41))" "→ ${symbolset[$(((2024 / 41) % 41))]}" "${symbolset[$((2024 % 41))]}"

output:
8 15 → j s

(edit)
All the calculations must be easily reversible so a ref number can be converted back into a sequence number for DB queries.

¹ simplicity in both the code and in the numbers generated.

  • Aquila@sh.itjust.works
    link
    fedilink
    English
    arrow-up
    2
    ·
    4 months ago

    Date sent plus database ID base64 encoded. Why make it so complicated?

    I.e 2024-15 becomes MjAyNC0xNQ==

    Can even drop any equals signs to make it less obvious it’s base64 if that’s a concern

    • LibreMonk@linkage.ds8.zoneOP
      link
      fedilink
      English
      arrow-up
      1
      ·
      edit-2
      4 months ago

      That is certainly a winner from the standpoint of code simplicity. And it’s trivially reversible. But I’m also prioritizing simplicity for human recipients above code simplicity. Base64 output is case sensitive and someone writing back and referencing a ref number would not necessarily preserve case. It’s also intolerant of human errors like confusing a “1” for a “l”.

      (edit) I think base32 would avoid the case sensitivity problem. So here’s a sample:

      for seq in {1..60}; do printf '%s → ' 2024-"$seq"; printf '%s\n' 2024-"$seq" | base32 | awk '{print tolower($1)}' | sed 's/=//g'; done
      
      output:
      2024-1  giydenbngefa
      2024-2  giydenbngifa
      2024-3  giydenbngmfa
      2024-4  giydenbngqfa
      2024-5  giydenbngufa
      2024-6  giydenbngyfa
      2024-7  giydenbng4fa
      2024-8  giydenbnhafa
      2024-9  giydenbnhefa
      2024-10  giydenbngeyau
      2024-11  giydenbngeyqu
      2024-12  giydenbngezau
      2024-13  giydenbngezqu
      2024-14  giydenbnge2au
      2024-15  giydenbnge2qu
      2024-16  giydenbnge3au
      2024-17  giydenbnge3qu
      2024-18  giydenbnge4au
      2024-19  giydenbnge4qu
      2024-20  giydenbngiyau
      2024-21  giydenbngiyqu
      2024-22  giydenbngizau
      2024-23  giydenbngizqu
      2024-24  giydenbngi2au
      2024-25  giydenbngi2qu