【发布时间】:2016-08-24 01:04:05
【问题描述】:
#!/bin/bash
# Program's Purpose: Compute time elapsed between epoch time and current time
# Produce an MD5 hash from that time
# Get code from that hash
# compute time elapsed in seconds between epoch time and current time
#EPOCH=$(date -u -d '2001-02-03 04:05:06' '+%F %H:%M:%S')
#CURRENT=$(date -u -d '2010-06-13 12:55:34' '+%F %H:%M:%S')
# code: dd15
EPOCH=$(date -u -d '1999-12-31 23:59:59' '+%F %H:%M:%S')
CURRENT=$(date -u -d '2013-05-06 07:43:25' '+%F %H:%M:%S')
# interval is time elapsed minus time elapsed % 60
echo $EPOCH
echo $CURRENT
read YEAR1 M1 DAY1 HOUR1 MIN1 SEC1 <<< "${EPOCH//[:-]/ }"
read YEAR2 M2 DAY2 HOUR2 MIN2 SEC2 <<< "${CURRENT//[:-]/ }"
echo $YEAR1 $M1 $DAY1 $HOUR1 $MIN1 $SEC1
# date in seconds from
sec1=$(date -d "$EPOCH" -u +%s)
sec2=$(date -d "$CURRENT" -u +%s)
echo $sec1
echo $sec2
# get the difference from the two times
difference=$((sec2 - sec1))
difference=$((difference - ((difference % 60))))
echo $difference
# get the hash from the time
hash=$(echo -n $difference | md5sum | tr -d '\n')
hash=$(echo -n $hash | md5sum | tr -d '\n')
echo $hash
# creating two strings, one with all of the letters
# the other with all of the numbers
letter=$(echo $hash | sed 's/[0-9]*//g' | cut -c1-2)
echo $letter
num=$(echo $hash | sed 's/[^0-9]*//g')
echo $num
#num=$(echo $num | cut -c1-2)
#echo $num
# getting the last two numbers in reverse order
num1=$(echo ${num: -1})
num=$(echo "${num::-1}")
echo $num
num2=$(echo ${num: -1})
code="$letter$num1$num2"
echo $code
我正在尝试编写一个需要一个纪元时间的程序,并且 当前时间,以秒为单位计算差异,然后创建一个 通过对时间进行双重 md5 哈希(以秒为单位)来编写代码。几点钟 我目前已经进入,秒差应该是421, 141、406,并且“代码”应该基于 60 秒 间隔,所以我试图生成的代码应该来自 421, 141、380。
生成的哈希应该是
042876ca07cb2d993601fb40a1525736,但我得到了
d49904f9e7e62d0ff16e523d89be08eb。谁能告诉我我在做什么
到底错了吗?
我读到 bash 在末尾留下一个换行符
字符串,所以我使用 -n 选项运行 echo 以删除该换行符,但我是
仍然没有得到首选的结果。
【问题讨论】:
-
你为什么要运行
md5sum两次?另外,什么String会产生您预期的输出? -
这是赋值的一部分,我们被告知 md5 (md5 (difference)),所以写为 'difference' 的变量回显到 md5 中,然后我将 'hash' 设置为md5 的差异。
-
哪个最终数字应该产生预期的哈希?
-
421141380 是插入以产生哈希的数字
标签: bash encryption hash md5 md5sum