安装
git clone git@github.com:guanzhi/GmSSL.git
mkdir build
cd build
cmake ..
make
make test
sudo make install
sudo ldconfig
SM3
echo -n "abc" | gmssl sm3
usage: sm3 [-hex|-bin] [-pubkey pem [-id str]] [-in file] [-out file]
输出结果:
66c7f0f462eeedd9d1f2d46bdc10e4e24167c4875cf2f7a2297da02b8f4ba8e0
SM4
加密
gmssl sm4 -cbc_sm3_hmac -decrypt \
-key 112233445566778811223344556677881122334455667788112233445566778811223344556677881122334455667788 \
-iv 11223344556677881122334455667788 -in ciphertext.bin
usage: sm4 (-cbc|-ctr|-gcm|-cbc_sm3_hmac|-ctr_sm3_hmac) {-encrypt|-decrypt} -key hex -iv hex [-in file] [-out file]
解密
gmssl sm4 -cbc_sm3_hmac -decrypt \
-key 112233445566778811223344556677881122334455667788112233445566778811223344556677881122334455667788 \
-iv 11223344556677881122334455667788 -in ciphertext.bin
usage: sm4 (-cbc|-ctr|-gcm|-cbc_sm3_hmac|-ctr_sm3_hmac) {-encrypt|-decrypt} -key hex -iv hex [-in file] [-out file]
输出结果:
hello
SM2
密钥生成
gmssl sm2keygen -pass 123 -out sm2.key -pubout pub.key
usage: sm2keygen Options
-pass pass Password to encrypt the private key
-out pem Output password-encrypted PKCS #8 private key in PEM format
-pubout pem Output public key in PEM format
输出私钥:sm2.key;公钥:pub.key
签名
gmssl sm2sign -key sm2.key -pass 123 -in test.txt -out sm2.sig
usage: sm2sign -key pem -pass str [-id str] [-in file] [-out file]
输出签名:sm2.sig
验签
gmssl sm2verify -pubkey pub.key -in test.txt -sig sm2.sig
usage: sm2verify (-pubkey pem | -cert pem) [-id str] [-in file] -sig file
输出:
verify : sucess
私钥签名,公钥验签
加密
echo "this world sucks, but you gonna like it!" >> world.txt
sm2encrypt -pubkey pub.key -in world.txt -out world.enc
usage: sm2encrypt (-pubkey pem | -cert pem) [-in file] [-out file]
输出:加密文件 world.enc
解密
gmssl sm2decrypt -key sm2.key -pass 123 -in world.enc -out world.dec
usage: sm2decrypt -key pem -pass str [-in file] [-out file]
输出:解密文件 world.dec
内容为
this world sucks, but you gonna like it!
公钥加密,私钥解密
产生随机数
gmssl rand -hex -outlen 10 -out randnum
usage: rand [-hex] [-rdrand] -outlen num [-out file]
输出文件:randnum
打开文件,显示
49688B89B9A0900E6D21