gpt4 book ai didi

linux - 使用 bash 和 HMAC 创建 TOTP

转载 作者:太空宇宙 更新时间:2023-11-04 10:06:12 25 4
gpt4 key购买 nike

我正在尝试编写一个生成 6 位数字 TOTP 的 bash 代码。我写了下面的代码:

##!/bin/bash
T=`date '+%Y%m%d%H%M'`
K="secret"
prefix="(stdin)= "
keyhex=$(echo -n $T | openssl dgst -sha1 -hmac $K | sed -e "s/^$prefix//")
dec=$((echo $(( 16#$keyhex )) ))
key=$((echo $(($dec % 1000000))))
echo $key

有时它可以工作,有时我会收到以下错误:

./auth.sh: line 6: echo 4076818289415231324 : syntax error in expression (error token is "4076818289415231324 ")
./auth.sh: line 7: % 1000000: syntax error: operand expected (error token is "% 1000000")

我做错了什么?

最佳答案

您正在尝试使用 $((arithmetic expansion)) 而您应该使用 $(command substitution):

代替

dec=$((echo $(( 16#$keyhex )) ))

使用

dec=$(echo $(( 16#$keyhex )) )

甚至更好,只是

dec=$(( 16#$keyhex ))

这是您的脚本,其中包含这些和其他一些调整:

#!/bin/bash
T=$(date '+%Y%m%d%H%M')
K="secret"
prefix="(stdin)= "
keyhex=$(printf '%s' "$K" | openssl dgst -sha1 -hmac $K | sed -e "s/^$prefix//")
dec=$(( 16#$keyhex ))
key=$((dec % 1000000))
echo "$key"

关于linux - 使用 bash 和 HMAC 创建 TOTP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52413559/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com