Calculate the file hash value and base64 encode it

# calculation file sha256, value $sha256sum filename f3b6d97538bf435399529405f5b5e88a81911d0439935ab82dd8c4c61df3d0df filename $ sha256sum filename | base64 ZjNiNmQ5NzUzOGJmNDM1Mzk5NTI5NDA1ZjViNWU4OGE4MTkxMWQwNDM5OTM1YWI4MmRkOGM0YzYx // Base64 RFC822 converts every three 8Bit characters into four 6Bit characters by adding a newline character per 76 characters. If the data is less than 3 bytes, 0 is used. (Base64 is usually shown as "=", so there may be at most 2 equals signs.) After # base64 character = (original character length + 2) / 3 * 4 # according to the above formula, sha256sum filename | base64 calculation results should be 44 bytes, but the actual calculated results 100 bytes is far greater than 44 bytes, The problem can be seen on the sha256sum # sha256sum hexadecimal data to the result of the output filename, we need the binary output in the form of $sha256sum filename | XXD - 00000000 b: 01100110 00110011 01100010 00110110 01100100 00111001 f3b6d9 00000006: 00110111 00110101 00110011 00111000 01100010 01100110 7538bf 0000000c: 00110100 00110011 00110101 00110011 00111001 00111001 435399 00000012: 00110101 00110010 00111001 00110100 00110000 00110101 529405 00000018: 01100110 00110101 01100010 00110101 01100101 00111000 f5b5e8 0000001e: 00111000 01100001 00111000 00110001 00111001 00110001 8a8191 00000024: 00110001 01100100 00110000 00110100 00110011 00111001 1d0439 0000002a: 00111001 00110011 00110101 01100001 01100010 00111000 935ab8 00000030: 00110010 01100100 01100100 00111000 01100011 00110100 2dd8c4 00000036: 01100011 00110110 00110001 01100100 01100110 00110011 c61df3 0000003c: 01100100 00110000 01100100 01100110 00100000 00100000 d0df 00000042: 01100011 01100111 01101111 00110000 00110001 00101110 cgo01. 00000048: C. # XXD is a binary view command. The -b parameter indicates that the output is a binary string, so you get binary output. Then we could calculate the base64 value $sha256sum filename | XXD - b | base64 MDAwMDAwMDA6IDAxMTAwMTEwIDAwMTEwMDExIDAxMTAwMDEwIDAwMTEwMTEwIDAxMTAwMTAwIDAw MTExMDAxICBmM2I2ZDkKMDAwMDAwMDY6IDAwMTEwMTExIDAwMTEwMTAxIDAw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EwICAgICAgICAgICAgICAgICAgICAg ICAgICAgICAgICAgICAgICBjLgo= # You can see that the output length is not 44 either, not as expected, check the XXD command to see that there are other parameters: # -ps represents a sequential hexadecimal dump output in PostScript, also known as a pure hexadecimal dump # -r represents a reverse conversion, To convert hexadecimal string representation for the actual characters $sha256sum filename | XXD - r - ps � � � u8 CS � r � � � � 芁 9 � � � Z - � � � � � � $sha256sum filename | XXD - r - ps | Base64 bzdti/Q1OZUpQF9bXoioGRHQQ5k1q4LdjExh3z0N8B # 87 the result is what we want right now as a result, the length of 44 # validation command is as follows: $echo "87 bzdti/Q1OZUpQF9bXoioGRHQQ5k1q4LdjExh3z0N8B" | base64 - d � � � u8 CS � R � � � � 芁 9 � � � Z - � � � � � �