将64位长十六进制数增加1

时间:2018-03-04 19:46:42

标签: bash cryptography hex

我有以下(最低)十六进制数:

0000000000000000000000000000000000000000000000000000000000000000

并希望将此数字增加1,直到达到最大值:

FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

我到目前为止这个剧本:

#!/bin/bash

for number in $( seq 1 255 )
do
  hex_representation=$( printf "%X" ${number} )
  echo "${number}: ${hex_representation}"
done

它几乎输出了我想要的东西(小规模,但它不是64位长的字符串。任何输入欢迎。所需的输出将是:

0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000001
0000000000000000000000000000000000000000000000000000000000000002
0000000000000000000000000000000000000000000000000000000000000003
...
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

2 个答案:

答案 0 :(得分:2)

使用正确的格式说明符,以及前导零字段宽度的修饰符(请参阅:printf(3))问题很简单:

#!/bin/bash

for number in $( seq 1 255 )
do
    printf "%064X\n" $number
done

示例使用/输出

$ bash 64bithex.sh
0000000000000000000000000000000000000000000000000000000000000001
0000000000000000000000000000000000000000000000000000000000000002
0000000000000000000000000000000000000000000000000000000000000003
0000000000000000000000000000000000000000000000000000000000000004
0000000000000000000000000000000000000000000000000000000000000005
0000000000000000000000000000000000000000000000000000000000000006
0000000000000000000000000000000000000000000000000000000000000007
0000000000000000000000000000000000000000000000000000000000000008
0000000000000000000000000000000000000000000000000000000000000009
000000000000000000000000000000000000000000000000000000000000000A
000000000000000000000000000000000000000000000000000000000000000B
000000000000000000000000000000000000000000000000000000000000000C
000000000000000000000000000000000000000000000000000000000000000D
000000000000000000000000000000000000000000000000000000000000000E
000000000000000000000000000000000000000000000000000000000000000F
0000000000000000000000000000000000000000000000000000000000000010
...
00000000000000000000000000000000000000000000000000000000000000FE
00000000000000000000000000000000000000000000000000000000000000FF

答案 1 :(得分:1)

如果你在谈论 64位数,你可以这样做:

#!/bin/bash

seq 1 18446744073709551615 | while read number
do
  hex_representation=$( printf "%016X" ${number} )
  echo "${number}: ${hex_representation}"
done

为此,必须进行两项更改。首先,因为在您的版本中$( seq 1 x )将尝试急切地评估表达式并将结果存储在内存中,所以必须使用while read number切换它,它会分别处理每一行。

其次,我将格式说明符扩展为"%016X",它将输出填充16个零。

如果您实际上在谈论 256位数(= 64位十六进制数字),您可以使用bc的任意精度算法来实现此目的。这是一个示例实现:

#!/bin/bash

seq 1 115792089237316195423570985008687907853269984665640564039457584007913129639935 | while read number
do
  hex_representation=$( printf "%64s" "$(echo "obase=16; ${number}" | bc)" | tr ' ' '0' )
  echo "${number}: ${hex_representation}"
done

您也可以在bc内部完成大部分工作:

#!/usr/bin/sh

echo """
obase=16
x = 0
while (x < 2^256) {
    x
    x += 1
}
""" | bc | while read hexnum; do
    printf "%64s\n" "$hexnum" | tr ' ' '0'
done

请注意,您不能使用%x格式说明符将数字转换为十六进制表示法,因为这需要bash支持256位数字。

%64s填充字符串的空格将被tr ' ' '0'替换为零。