linuxbashcgiuniqueidentifierinitialization-vector

Generate a unique IV header


I need to generate a unique IV (Initialization Vector) header.

The IV SHALL be 96 bits long, comprised of the year (16 bits, 0 to 65535), the current time (64 bits, nanoseconds this year), and a counter (16 bits) to generate a 96-bit IV.

How can I generate this unique identifier in CGI bash script?


Solution

  • in y2.sh:

    #!/bin/bash
    d2b() {
        printf "%0${2}s\n" $(bc <<< "obase=2;$1")|tr ' ' '0'
    }
    
    counter=1
    eval "$(date +'firstday=%Y-01-01 now=%s nano=%N year=%Y')"
    midnight=$(date -d "$firstday 0" +%s)
    seconds=$((now - midnight))
    header=$(d2b $year 16)$(d2b $seconds$nano 64)$(d2b $counter 16)
    echo header: $header
    

    run it:

    $ ./y2.sh
    header: 000001111110000000000000011000100101000110110001011001011100011000011100010000010000000000000001