Shashank Shekher Shashank Shekher - 19 days ago 6
Linux Question

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
?

Answer

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
  • function() { ... } - define a custom function in bash
  • bc <<< "obase=2;$1 - decimal to binary the 1st parameter with sed
  • printf "%0${2}s\n" | ... | tr - fill with leading zeroes to length $2
  • eval date... - put the current time into variables
  • date -d "$firstday 0" +%s - unixtime at jan1 00:00 (seconds)

run it:

$ ./y2.sh
header: 000001111110000000000000011000100101000110110001011001011100011000011100010000010000000000000001
Comments