ec_sign_info_file="/tmp/ec_sign_info_file."$time_stamp ec_sign_info_sha256="/tmp/ec_sign_info_sha256."$time_stamp ec_binary_sign_file="/tmp/ec_binary_sign_file."$time_stamp /bin/echo -n "$1"_"$2" > $ec_sign_info_file openssl dgst -sha256 -binary -out $ec_sign_info_sha256 $ec_sign_info_file CheckStop $LINENO openssl pkeyutl -sign -in $ec_sign_info_sha256 -out $ec_binary_sign_file -inkey $3 -keyform PEM CheckStop $LINENO openssl base64 -e -in $ec_binary_sign_file -out $4 CheckStop $LINENO #rm $ec_sign_info_file $ec_sign_info_sha256 $ec_binary_sign_file echo "gen_ecdsa_sign succ sign_file_path:"$4
时间: 2023-08-12 17:07:26 浏览: 150
这是一个使用OpenSSL库在Linux上实现ECDSA签名的脚本的一部分,它包括以下步骤:
1. 创建一个临时文件来存储要签名的信息。
2. 使用OpenSSL的dgst命令计算信息的SHA256哈希值,并将其写入另一个临时文件。
3. 使用OpenSSL的pkeyutl命令使用私钥对哈希进行签名,并将签名写入第三个临时文件。
4. 使用OpenSSL的base64命令将签名编码为Base64格式,并将其写入最终的签名文件。
5. 最后,这个脚本打印一个成功消息,并返回签名文件的路径。
请注意,这个脚本仅是ECDSA签名的示例,并且在实际应用中需要更多的安全措施和步骤。
相关问题
转python写法:#!/bin/sh time_stamp=`date +%s` function CheckStop() { if [ $? -ne 0 ]; then echo "execute fail, error on line_no:"$1" exit!!!" exit fi } function GenEcdsaKey() { ec_param_file_path="/tmp/ec_param.pem."$time_stamp openssl ecparam -out $ec_param_file_path -name prime256v1 -genkey CheckStop $LINENO openssl genpkey -paramfile $ec_param_file_path -out $1 CheckStop $LINENO openssl pkey -in $1 -inform PEM -out $2 -outform PEM -pubout CheckStop $LINENO rm $ec_param_file_path echo "gen_ecdsa_key succ prikey_path:"$1" pubkey_path:"$2 } function GenEcdsaSign() { ec_sign_info_file="/tmp/ec_sign_info_file."$time_stamp ec_sign_info_sha256="/tmp/ec_sign_info_sha256."$time_stamp ec_binary_sign_file="/tmp/ec_binary_sign_file."$time_stamp echo -n "$1"_"$2" > $ec_sign_info_file openssl dgst -sha256 -binary -out $ec_sign_info_sha256 $ec_sign_info_file CheckStop $LINENO openssl pkeyutl -sign -in $ec_sign_info_sha256 -out $ec_binary_sign_file -inkey $3 -keyform PEM CheckStop $LINENO openssl base64 -e -in $ec_binary_sign_file -out $4 CheckStop $LINENO rm $ec_sign_info_file $ec_sign_info_sha256 $ec_binary_sign_file echo "gen_ecdsa_sign succ sign_file_path:"$4 } function VerifyEcdsaSign() { ec_sign_info_file="/tmp/ec_sign_info_file."$time_stamp ec_sign_info_sha256="/tmp/ec_sign_info_sha256."$time_stamp ec_binary_sign_file="/tmp/ec_binary_sign_file."$time_stamp echo -n "$1"_"$2" > $ec_sign_info_file openssl dgst -sha256 -binary -out $ec_sign_info_sha256 $ec_sign_info_file CheckStop $LINENO openssl base64 -d -in $4 -out $ec_binary_sign_file CheckStop $LINENO openssl pkeyutl -verify -in $ec_sign_info_sha256 -sigfile $ec_binary_sign_file -pubin -inkey $3 -keyform PEM rm $ec_sign_info_file $ec_sign_info_sha256 $ec_binary_sign_file } function Usage() { echo "Usage:" echo "mmiot_ecdsa_sign.sh gen_ecdsa_key <private_key_file_path> <public_key_file_path>" echo "mmiot_ecdsa_sign.sh gen_ecdsa_sign <product_id> <sn> <private_
以下是转换后的 Python 代码:
```python
#!/usr/bin/env python
import os
import subprocess
import base64
time_stamp = str(int(time.time()))
def check_stop(line_no, ret):
if ret != 0:
print("execute fail, error on line_no: ", line_no, " exit!!!")
exit()
def gen_ecdsa_key(private_key_file_path, public_key_file_path):
ec_param_file_path = "/tmp/ec_param.pem." + time_stamp
ret = subprocess.call(["openssl", "ecparam", "-out", ec_param_file_path, "-name", "prime256v1", "-genkey"])
check_stop(sys._getframe().f_lineno, ret)
ret = subprocess.call(["openssl", "genpkey", "-paramfile", ec_param_file_path, "-out", private_key_file_path])
check_stop(sys._getframe().f_lineno, ret)
ret = subprocess.call(["openssl", "pkey", "-in", private_key_file_path, "-inform", "PEM", "-out", public_key_file_path, "-outform", "PEM", "-pubout"])
check_stop(sys._getframe().f_lineno, ret)
os.remove(ec_param_file_path)
print("gen_ecdsa_key succ prikey_path:", private_key_file_path, " pubkey_path:", public_key_file_path)
def gen_ecdsa_sign(product_id, sn, private_key_file_path, sign_file_path):
ec_sign_info_file = "/tmp/ec_sign_info_file." + time_stamp
ec_sign_info_sha256 = "/tmp/ec_sign_info_sha256." + time_stamp
ec_binary_sign_file = "/tmp/ec_binary_sign_file." + time_stamp
with open(ec_sign_info_file, 'w') as f:
f.write(product_id + "_" + sn)
ret = subprocess.call(["openssl", "dgst", "-sha256", "-binary", "-out", ec_sign_info_sha256, ec_sign_info_file])
check_stop(sys._getframe().f_lineno, ret)
ret = subprocess.call(["openssl", "pkeyutl", "-sign", "-in", ec_sign_info_sha256, "-out", ec_binary_sign_file, "-inkey", private_key_file_path, "-keyform", "PEM"])
check_stop(sys._getframe().f_lineno, ret)
with open(ec_binary_sign_file, 'rb') as f:
sign_binary = f.read()
sign_base64 = base64.b64encode(sign_binary).decode()
with open(sign_file_path, 'w') as f:
f.write(sign_base64)
os.remove(ec_sign_info_file)
os.remove(ec_sign_info_sha256)
os.remove(ec_binary_sign_file)
print("gen_ecdsa_sign succ sign_file_path:", sign_file_path)
def verify_ecdsa_sign(product_id, sn, public_key_file_path, sign_file_path):
ec_sign_info_file = "/tmp/ec_sign_info_file." + time_stamp
ec_sign_info_sha256 = "/tmp/ec_sign_info_sha256." + time_stamp
ec_binary_sign_file = "/tmp/ec_binary_sign_file." + time_stamp
with open(ec_sign_info_file, 'w') as f:
f.write(product_id + "_" + sn)
ret = subprocess.call(["openssl", "dgst", "-sha256", "-binary", "-out", ec_sign_info_sha256, ec_sign_info_file])
check_stop(sys._getframe().f_lineno, ret)
with open(sign_file_path, 'r') as f:
sign_base64 = f.read()
sign_binary = base64.b64decode(sign_base64)
with open(ec_binary_sign_file, 'wb') as f:
f.write(sign_binary)
ret = subprocess.call(["openssl", "pkeyutl", "-verify", "-in", ec_sign_info_sha256, "-sigfile", ec_binary_sign_file, "-pubin", "-inkey", public_key_file_path, "-keyform", "PEM"])
os.remove(ec_sign_info_file)
os.remove(ec_sign_info_sha256)
os.remove(ec_binary_sign_file)
print("verify_ecdsa_sign result:", "succ" if ret == 0 else "fail")
if len(sys.argv) < 2:
print("Usage:")
print("python mmiot_ecdsa_sign.py gen_ecdsa_key <private_key_file_path> <public_key_file_path>")
print("python mmiot_ecdsa_sign.py gen_ecdsa_sign <product_id> <sn> <private_key_file_path> <sign_file_path>")
print("python mmiot_ecdsa_sign.py verify_ecdsa_sign <product_id> <sn> <public_key_file_path> <sign_file_path>")
exit()
if sys.argv[1] == "gen_ecdsa_key":
gen_ecdsa_key(sys.argv[2], sys.argv[3])
elif sys.argv[1] == "gen_ecdsa_sign":
gen_ecdsa_sign(sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5])
elif sys.argv[1] == "verify_ecdsa_sign":
verify_ecdsa_sign(sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5])
else:
print("Usage:")
print("python mmiot_ecdsa_sign.py gen_ecdsa_key <private_key_file_path> <public_key_file_path>")
print("python mmiot_ecdsa_sign.py gen_ecdsa_sign <product_id> <sn> <private_key_file_path> <sign_file_path>")
print("python mmiot_ecdsa_sign.py verify_ecdsa_sign <product_id> <sn> <public_key_file_path> <sign_file_path>")
```
//计算里程计四元数 tf2::Quaternion odom_quat; odom_quat.setRPY(0,0,pos_data_.angular_z); //获取数据 odom_msgs_.header.stamp = ros::Time::now(); odom_msgs_.header.frame_id = odom_frame_; odom_msgs_.child_frame_id = base_frame_; odom_msgs_.pose.pose.position.x = pos_data_.pos_x; odom_msgs_.pose.pose.position.y = pos_data_.pos_y; odom_msgs_.pose.pose.position.z = 0; //高度为0 odom_msgs_.pose.pose.orientation.x = odom_quat.getX(); odom_msgs_.pose.pose.orientation.y = odom_quat.getY(); odom_msgs_.pose.pose.orientation.z = odom_quat.getZ(); odom_msgs_.pose.pose.orientation.w = odom_quat.getW(); odom_msgs_.twist.twist.linear.x = vel_data_.linear_x; odom_msgs_.twist.twist.linear.y = vel_data_.linear_y; odom_msgs_.twist.twist.angular.z = vel_data_.angular_z;
这段代码实现了计算里程计信息的功能。具体来说,它通过获取位置和速度信息,计算出里程计四元数和速度信息,并将其存储到 ROS 的 odom 消息中。
首先,通过调用 setRPY 函数,根据角度信息计算出旋转四元数 odom_quat。其中,pos_data_.angular_z 表示机器人绕 Z 轴的角度,0 表示机器人在 XY 平面上运动,没有绕 X 轴和 Y 轴转动。
然后,将里程计信息存储到 odom 消息中。其中,header 字段表示消息头信息,包括时间戳和坐标系信息;pose 字段表示机器人在全局坐标系下的位置和姿态信息;twist 字段表示机器人的速度信息。
具体来说,odom_frame_ 表示全局坐标系的名称,base_frame_ 表示机器人坐标系的名称。pos_data_.pos_x 和 pos_data_.pos_y 表示机器人在全局坐标系下的 X 和 Y 坐标,同时将高度设置为 0。odom_quat.getX()、odom_quat.getY()、odom_quat.getZ() 和 odom_quat.getW() 分别表示旋转四元数的四个分量。
vel_data_ 表示机器人的速度信息,其中 linear_x 和 linear_y 分别表示机器人在 X 和 Y 方向上的线速度,angular_z 表示机器人绕 Z 轴的角速度。
最后,将里程计信息存储到 odom 消息中,并发布到 ROS 系统中,以供其它模块使用。
阅读全文