md5sum and random number creation in shell programming

Keywords: OpenSSL Firefox network Linux

1. What is md5sum

MD5 full name is message digest algorithm. This algorithm calculates information of any length bit by bit, and generates a "fingerprint" (or "message digest") with a binary length of 128 bits (hex length is 32 bits). The possibility of different files generating the same message digest is very small.

The md5sum command uses the MD5 message digest algorithm (128 bits) to calculate and check the checksums of files. Generally speaking, after installing Linux, there will be md5sum, which runs directly on the command line terminal.

md5sum of Linux commands
Data inconsistency may occur before and after transmission in network transmission, transfer between devices, copying large files, etc. This kind of situation is easy to appear in the relatively unstable environment of network. So it is imperative to verify the integrity of documents.
The md5sum command is used to generate and verify the md5 value of a file. It checks the contents of the file bit by bit. Is the content of the file, independent of the file name, that is, the same file content, the same md5 value
md5 value is a 128 bit binary data, converted to hexadecimal is a 32 (128 / 4) bit binary value

md5 verification, there is a very small probability that different files may generate the same md5. More secure verification algorithm than md5, SHA * series
During network transmission, we verify that the source file gets its md5sum, and then verify its target file after transmission. If the source file is consistent with the target file md5, it means that there is no exception in file transmission. Otherwise, the file is not transferred correctly during transmission

2. example

[root@localhost md5sum]# date > data
[root@localhost md5sum]# cat data
Sat Feb 15 21:57:08 EST 2020
[root@localhost md5sum]# md5sum data
d0812de959953bbb0644b29c3a423ada  data
[root@localhost md5sum]# md5sum data |cut -d' ' -f1
d0812de959953bbb0644b29c3a423ada
[root@localhost md5sum]# 

[root@localhost md5sum]# ls
data  data1  data2  data3
[root@localhost md5sum]# md5sum *
d0812de959953bbb0644b29c3a423ada  data
f602f7bf882860efc88df88e0864336e  data1
0a4fd8b099ae157b4e52cc14bd154901  data2
f953a05af29851d2c596c188ffb720a5  data3
[root@localhost md5sum]# file data
data: ASCII text
[root@localhost md5sum]# md5sum -b data
d0812de959953bbb0644b29c3a423ada *data
[root@localhost md5sum]# 

Redirect md5 to the specified file

[root@localhost md5sum]# md5sum data > data.md5
[root@localhost md5sum]# cat data.md5 
d0812de959953bbb0644b29c3a423ada  data
[root@localhost md5sum]# md5sum data
d0812de959953bbb0644b29c3a423ada  data
[root@localhost md5sum]# 

Redirect or append md5 of multiple files to the specified file

[root@localhost md5sum]# md5sum * > d.md5
[root@localhost md5sum]# cat d.md5 
d0812de959953bbb0644b29c3a423ada  data
f602f7bf882860efc88df88e0864336e  data1
0a4fd8b099ae157b4e52cc14bd154901  data2
f953a05af29851d2c596c188ffb720a5  data3
2a892aa86403e1995cf36ec62f91416b  data.md5
[root@localhost md5sum]# ls
data  data1  data2  data3  data.md5  d.md5
[root@localhost md5sum]# 
[root@localhost md5sum]# md5sum data3 >> d.md5 
[root@localhost md5sum]# cat d.md5 
d0812de959953bbb0644b29c3a423ada  data
f602f7bf882860efc88df88e0864336e  data1
0a4fd8b099ae157b4e52cc14bd154901  data2
f953a05af29851d2c596c188ffb720a5  data3
2a892aa86403e1995cf36ec62f91416b  data.md5
f953a05af29851d2c596c188ffb720a5  data3
[root@localhost md5sum]# 

-c verify the existing file according to the generated md value

[root@localhost md5sum]# cat d.md5 
d0812de959953bbb0644b29c3a423ada  data
f602f7bf882860efc88df88e0864336e  data1
0a4fd8b099ae157b4e52cc14bd154901  data2
f953a05af29851d2c596c188ffb720a5  data3
2a892aa86403e1995cf36ec62f91416b  data.md5
f953a05af29851d2c596c188ffb720a5  data3
[root@localhost md5sum]# date >> data
[root@localhost md5sum]# md5sum -c d.md5 
data: FAILED
data1: OK
data2: OK
data3: OK
data.md5: OK
data3: OK
md5sum: WARNING: 1 computed checksum did NOT match
[root@localhost md5sum]# 

– status does not generate prompt information, which is judged by the return value of the command

[root@localhost md5sum]# md5sum -c --status d.md5 
[root@localhost md5sum]# echo $?
1
[root@localhost md5sum]# 

3. How to create random number in shell

$RANDOM generated

[root@localhost md5sum]# echo $RANDOM
7575
[root@localhost md5sum]# echo $RANDOM
14127
[root@localhost md5sum]# echo $RANDOM
11277
[root@localhost md5sum]# 

Encrypt with string and write a key you know

[root@localhost md5sum]# echo "westos-yrx$RANDOM" |md5sum 
aa225b928a1646180ed9c1e12fddece4  -
[root@localhost md5sum]# echo "westos-yrx$RANDOM" |md5sum |cut -c 8-15
27479c08
[root@localhost md5sum]# 

openssl generates random number
How many bits does the last digit represent

[root@localhost md5sum]# openssl rand -base64 8
qRE8+SYni6w=
[root@localhost md5sum]# openssl rand -base64 80
ZxZX6b5yzwJTjrSFjrryZifsrB4Biow+ifCVk9OjRCyKJ4Giev4wEfB6F6RyXlIU
0jO/zv8eal2b1aJz/kGeNySPu5WTWPg+UdTszOOPW48=
[root@localhost md5sum]# 

date generation

[root@localhost md5sum]# date +%s
1581823237
[root@localhost md5sum]# date +%s%N
1581823245378766270
[root@localhost md5sum]# 

date +%s / / the number of seconds (time stamp) since January 1, 1970, 00:00:00 UTC

In the aspect of date display, the user can set the format to be displayed. The format is set as a plus sign followed by several tags. The list of available tags is as follows:
Time:

%: print%
%n: next line
 %t: Jumping lattice
 %H: hours (00.. 23)
%I: hours (01.. 12)
%k: hour (0.. 23)
%l: hour (1.. 12)
%M: minutes (00.. 59)
%p: display local AM or PM
 %r: direct display time (12 hour system, format hh:mm:ss [AP]M)
%s: seconds since January 1, 1970, 00:00:00 UTC
 %N: Billionths of a second, nanosecond [000000000999999]
%S: second (00.. 61)
%T: direct display time (24-hour system)
%X: equivalent to% H:%M:%S
 %Z: display time zone

Date:

%a: day of the week (Sun..Sat)
%A: Sunday.. Saturday
 %b: month (Jan..Dec)
%B: month.. December
 %c: display date and time directly
 %d: Day (01.. 31)
%D: direct display date (mm/dd/yy)
%h: same as%b
 %j: day of the year (001.. 366)
%m: month (01.. 12)
%U: week of the year (00.. 53) (with Sunday as the first day of the week)
%w: day of the week (0.. 6)
%W: the first day of the year (00.. 53)
%x: direct display date (mm/dd/yy)
%y: last two digits of the year (00.99)
%Y: full year (0000.. 9999)
[root@localhost md5sum]# date +%j
046
[root@localhost md5sum]# date +%x
02/15/2020
[root@localhost md5sum]# date +%U
06
[root@localhost md5sum]# date +%D
02/15/20
[root@localhost md5sum]# date +%A
Saturday
[root@localhost md5sum]# date +%X
10:24:34 PM
[root@localhost md5sum]# date +%T
22:24:45
[root@localhost md5sum]# date +%r
10:24:57 PM
[root@localhost md5sum]# 

4. Practical training

[kiosk@foundation8 firefox]$ openssl rand -base64 40
Xl6h4nDyXJQwtIwQGCFNeO6sPBMWsMK9EVhsvySNLTjugtM+wJjaKg==
[kiosk@foundation8 firefox]$ openssl rand -base64 40 |sed 's/[^a-z]//g'
izjnzbnytzqrvlbcnurqpw
[kiosk@foundation8 firefox]$ openssl rand -base64 40 |sed 's/[^a-z]//g'|cut -c 2-11
omncdezywj
[kiosk@foundation8 firefox]$
[root@localhost practice]# sh 01.sh 
[root@localhost practice]# ls /westos/
ahlovfzacp_westos.html  hegegfqhea_westos.html  sbnxrfgkkn_westos.html
bnonstigyj_westos.html  iwtynghpeh_westos.html  ulkkljusud_westos.html
giympdvnpt_westos.html  jjutdxxnmv_westos.html
guttqayrqs_westos.html  ostnxjjqwy_westos.html
[root@localhost practice]# cat ~/practice/01.sh 
#!/bin/bash
Path=/westos
[ -d "$Path" ] || mkdir -p $Path   #Determine whether the directory exists, and create if it does not exist
for n in `seq 10`
do
	random=$(openssl rand -base64 40 |sed 's/[^a-z]//g' |cut -c 2-11)
	touch $Path/${random}_westos.html
done

[root@localhost practice]# 
[kiosk@foundation8 firefox]$ seq 10
1
2
3
4
5
6
7
8
9
10
[kiosk@foundation8 firefox]$ seq -w 10
01
02
03
04
05
06
07
08
09
10
#!/bin/bash
user="westos"
passfile="/tmp/user.log"
for num in `seq -w 10` # num is 01-10 in turn
do
        pass="`echo $RANDOM |md5sum |cut -c 3-11`" #Password
        useradd $user$num &> /dev/null && {
        echo "$pass" | passwd --stdin $user$num &> /dev/null
        echo -e "user: $user$num\tpasswd: $pass">> $passfile
}
if [ $? -eq 0 ];then
        echo "$user$num is ok"
else
        echo "$user$num is not ok"
fi
done
echo "#########################"
cat $passfile && >$passfile
Published 128 original articles, won praise 1, visited 1827
Private letter follow

Posted by damo87 on Sat, 15 Feb 2020 21:09:27 -0800