标签 shell 下的文章

hexdump 查看二进制文件

echo "https://const.net.cn" > bin.data
hesy@hesy-ThinkPad-P15v-Gen-1:~/2021/linux$ hexdump -C bin.data 
00000000  68 74 74 70 73 3a 2f 2f  63 6f 6e 73 74 2e 6e 65  |https://const.ne|
00000010  74 2e 63 6e 0a                                    |t.cn.|
00000015

hexdump 自定义格式显示二进制文件

hexdump -e '4/1 "%02X " "\n"' bin.data 
68 74 74 70
73 3A 2F 2F
63 6F 6E 73
74 2E 6E 65
74 2E 63 6E
0A         
hexdump -e '8/1 "%02X " "\n"' bin.data 
68 74 74 70 73 3A 2F 2F
63 6F 6E 73 74 2E 6E 65
74 2E 63 6E 0A         
hexdump -e '16/1 "%02X " "\n"' bin.data 
68 74 74 70 73 3A 2F 2F 63 6F 6E 73 74 2E 6E 65
74 2E 63 6E 0A                                 
hexdump -e '16/1 "%_p " "\n"' bin.data 
h t t p s : / / c o n s t . n e
t . c n .           
hexdump -e '16/1 "%03d " "\n"' bin.data 
104 116 116 112 115 058 047 047 099 111 110 115 116 046 110 101
116 046 099 110 010 

hexdump 将二进制文件转为十六进制文件

hexdump -e '16/1 "%02X " "\n"' bin.data > hex.data
xxd -r -p hex.data bin.data.1
md5sum *
84d86c7c482190d90838c5de11d99d06  bin.data
84d86c7c482190d90838c5de11d99d06  bin.data.1
a100e4d2a6bcf2faceb56f106dfb1c6b  hex.data

xxd 查看二进制文件

xxd -c 4 -p bin.data 
68747470
733a2f2f
636f6e73
742e6e65
742e636e
0a
xxd -c 8 -p bin.data 
68747470733a2f2f
636f6e73742e6e65
742e636e0a
xxd -c 16 -p bin.data 
68747470733a2f2f636f6e73742e6e65
742e636e0a

xxd 将十六进制文件转为二进制文件

xxd -r -p input.txt output.bin    
xxd -c 16 -p bin.data > hex.data
xxd -r -p hex.data bin.data.1
md5sum *
84d86c7c482190d90838c5de11d99d06  bin.data
84d86c7c482190d90838c5de11d99d06  bin.data.1
2d8c913c2685ef14e39fa97a28c3ad79  hex.data

                        

OpenSSL 生成 SM2 密钥

openssl ecparam -name SM2 -genkey -out sm2_ec.key 
cat sm2_ec.key 
-----BEGIN EC PARAMETERS-----
BggqgRzPVQGCLQ==
-----END EC PARAMETERS-----
-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIB9dGHE5+6AD9DGmA8g/cEqn8HYTMBhbM+g2XJ16RqZ1oAoGCCqBHM9V
AYItoUQDQgAEJg19rra1BeuYx9ZU1GbfD0ceE9X67/c2hdb6XZLQor5oNVa+o9HZ
WBioc1hNCC2avO1Dpg5ZAb2YsS71TT7Bsw==
-----END EC PRIVATE KEY-----

OpenSSL 根据SM2私钥生成公钥

openssl ec -in sm2_ec.key -pubout -out sm2_ec.pubkey 
read EC key
writing EC key
cat sm2_ec.pubkey 
-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAEJg19rra1BeuYx9ZU1GbfD0ceE9X6
7/c2hdb6XZLQor5oNVa+o9HZWBioc1hNCC2avO1Dpg5ZAb2YsS71TT7Bsw==
-----END PUBLIC KEY-----

OpenSSL SM3 计算文件Hash

echo "https://const.net.cn" > sign.data 
openssl dgst -SM3 sign.data 
SM3(sign.data)= 8c13610aeb3040b2899ac224ae7db0710030803c424f776e7241340c66a6d553

OpenSSL 使用 SM2 签名文件

openssl dgst -SM3 -sign sm2_ec.key -out sm2_ec.sig sign.data 
Error setting context
140524048778560:error:100C508A:elliptic curve routines:pkey_ec_ctrl:invalid digest type:../crypto/ec/ec_pmeth.c:331:

在当前版本(OpenSSL 1.1.1f)还不支持命令行使用SM2结合SM3签名。将hash算法换成sha256试试。
OpenSSL 使用 SM2 结合 sha256签名

openssl dgst -sha256 -sign sm2_ec.key -out sm2_ec.sig sign.data 
root@hesy-ThinkPad-P15v-Gen-1:/home/hesy/2021/asn1/src/sm# hexdump -C sm2_ec.sig 
00000000  30 46 02 21 00 d7 52 c2  63 a3 12 ff ef af 69 8e  |0F.!..R.c.....i.|
00000010  8a 35 17 9f f2 0c e2 b1  80 fb dd a1 38 a3 59 14  |.5..........8.Y.|
00000020  5a 18 33 ba 43 02 21 00  9d 10 91 a7 5f a4 cf bb  |Z.3.C.!....._...|
00000030  7b 75 c0 27 17 d5 2d 55  09 cc 10 49 29 f8 bc 0d  |{u.'..-U...I)...|
00000040  10 d6 02 db b1 e4 7c 7a                           |......|z|
00000048

OpenSSL 使用 SM2 结合 sha256 验签

openssl dgst -sha256 -verify sm2_ec.pubkey -signature sm2_ec.sig sign.data 
Verified OK

OpenSSL 使用 SM2 结合 SM3 验签

openssl dgst -SM3 -verify sm2_ec.pubkey -signature sm2_ec.sig sign.data 
Error setting context
140471948707136:error:100C508A:elliptic curve routines:pkey_ec_ctrl:invalid digest type:../crypto/ec/ec_pmeth.c:331:

同上面一样的原因,官方没实现,命令行用不了。

1、curl -w参数

curl -h |grep "write-out"

-w, --write-out <format> Use output FORMAT after completion

2、显示所有时间示例

curl -o /dev/null -s -w "time_namelookup:%{time_namelookup}\ntime_connect: %{time_connect}\ntime_starttransfer: %{time_starttransfer}\ntime_total: %{time_total}\n"  "https://const.net.cn/"

输出:

time_namelookup:0.058759
time_connect: 0.069912
time_starttransfer: 0.147355
time_total: 0.167685

3、参数说明

time_namelookup: DNS 服务器域名解析的时间(以上时间单位都是s)
time_connect: client 发出请求,到 c/s 建立TCP 的时间;里面包括 DNS 解析的时间
time_starttransfer: client 发出请求;到 server 响应发出第一个字节开始的时间;包括前面的2个时间
time_total: 从请求发起到链接关闭的总耗时

4、官方英文说明

time_appconnect: The time, in seconds, it took from the start until the SSL/SSH/etc connect/handshake to the remote host was completed. (Added in 7.19.0)
time_connect: The time, in seconds, it took from the start until the TCP connect to the remote host (or proxy) was completed.
time_namelookup: The time, in seconds, it took from the start until the name resolving was completed.
time_pretransfer: The time, in seconds, it took from the start until the file transfer was just about to begin. This includes all pre-transfer commands and negotiations that are specific to the particular protocol(s) involved.
time_redirect: The time, in seconds, it took for all redirection steps including name lookup, connect, pretransfer and transfer before the final transaction was started. time_redirect shows the complete execution time for multiple redirections. (Added in 7.12.3)
time_starttransfer: The time, in seconds, it took from the start until the first byte was just about to be transferred. This includes time_pretransfer and also the time the server needed to calculate the result.
time_total: The total time, in seconds, that the full operation lasted.

5、格式可以使用字符串,格式化文件以及标准输入

The format can be specified as a literal "string", or you can have curl read the format from a file with "@filename" and to tell curl to read the format from stdin you write "@-".

6、使用格式文件示例

cat curl-format.txt

   time_namelookup:%{time_namelookup}\n
      time_connect:%{time_connect}\n
   time_appconnect:%{time_appconnect}\n
     time_redirect:%{time_redirect}\n    
  time_pretransfer:%{time_pretransfer}\n  
time_starttransfer:%{time_starttransfer}\n
------------------------------\n
        time_total:  %{time_total}\n

使用示例:

curl -o /dev/null -s -w "@curl-format.txt"  "https://const.net.cn/"

输出结果:

   time_namelookup:0.165160
      time_connect:0.240436
   time_appconnect:0.326137
     time_redirect:0.000000
  time_pretransfer:0.326211
time_starttransfer:0.412927
------------------------------
        time_total:  0.486244

1、使用默认的 归档管理器 打开
使用提取到此处功能,如果可以就不用其他办法了。
有时候会不哪么好使,会提示错误。
2、安装rar

sudo apt install rar
sudo apt install unrar

3、解压使用 rar x file.rar
示例:

rar x "孙露《2021最新专辑·再远的远方》.rar"

输出:

RAR 5.50 Copyright (c) 1993-2017 Alexander Roshal 11 Aug 2017
Trial version Type 'rar -?' for help

Extracting from 孙露《2021最新专辑·再远的远方》.rar

Creating 孙露《2021最新专辑·再远的远方》[WAV分轨]
OK Extracting 孙露《2021最新专辑·再远的远方》[WAV分轨]/01.孙露 - 再远的远方.wav
OK Extracting 孙露《2021最新专辑·再远的远方》[WAV分轨]/02.孙露 - 迷人月光.wav
OK Extracting 孙露《2021最新专辑·再远的远方》[WAV分轨]/03.孙露 - 心软.wav
OK Extracting 孙露《2021最新专辑·再远的远方》[WAV分轨]/04.孙露 - 不忘.wav
OK Extracting 孙露《2021最新专辑·再远的远方》[WAV分轨]/05.孙露 - 你敢不敢.wav
OK Extracting 孙露《2021最新专辑·再远的远方》[WAV分轨]/06.孙露 - 好好爱自己.wav
OK Extracting 孙露《2021最新专辑·再远的远方》[WAV分轨]/07.孙露 - 破晓.wav
OK Extracting 孙露《2021最新专辑·再远的远方》[WAV分轨]/08.孙露 - 呓语.wav
OK Extracting 孙露《2021最新专辑·再远的远方》[WAV分轨]/09.孙露 - 眼泪会说谎.wav
OK Extracting 孙露《2021最新专辑·再远的远方》[WAV分轨]/10.孙露 - 爱的太假.wav
OK Extracting 孙露《2021最新专辑·再远的远方》[WAV分轨]/Cover.jpg
OK

1、查看curl与时间相关的选项

curl -h |grep time

有用的输出

--connect-timeout <seconds> Maximum time allowed for connection
--expect100-timeout <seconds> How long to wait for 100-continue
--keepalive-time <seconds> Interval time for keepalive probes
-m, --max-time <seconds> Maximum time allowed for the transfer
-y, --speed-time <seconds> Trigger 'speed-limit' abort after this time

2、控制连接超时--connect-timeout 5

curl -v --connect-timeout 1 https://const.net.cn

当出现连接超时时,会提示如下信息

curl: (28) Connection timed out after 1001 milliseconds

3、总超时时间--max-time
 我们希望curl程序在运行一段时间后,不管有没有执行完成都能正常退出。就用这个max-time

curl -v -m 60 https://const.net.cn

4、--speed-time 的用法
speed-time是用来根据速率来控制连接是否断开的。
用法:

curl -o const.log "https://const.net.cn" --speed-time 5 --speed-limit 1

是说将url内容保存到const.log中, 如果传输速度小于1字节/秒的状态持续5秒,该连接就会断开,并退出程序。