我已经使用tokiostream.write_all(sign.as_bytes()).await.unwrap();
发送了签名字节流作为字符串
在客户端,我得到它作为字符串,说:
[132, 40, 243, 14, 49, 231, 64, 130, 236, 226, 5, 180, 166, 50, 2, 49, 124, 248, 235, 53, 7, 204, 124, 101, 54, 230, 225, 25, 201, 47, 159, 52]
现在要用公钥验证签名,我需要将其用作字节(我使用的crate不允许将签名用作字符串)但问题是,当我将符号转换为字节(as_bytes()
)时,它将字符串转换为字节流,例如:
[91, 49, 51, 50, 44, 32, 52, 48, 44, 32, 50, 52, 51, 44, 32, 49, 52, 44, 32, 52, 57, 44, 32, 50, 51, 49, 44, 32, 54, 52, 44, 32, 49, 51, 48, 44, 32, 50, 51, 54, 44, 32, 50, 50, 54, 44, 32, 53, 44, 32, 49, 56, 48, 44, 32, 49, 54, 54, 44, 32, 53, 48, 44, 32, 50, 44, 32, 52, 57, 44, 32, 49, 50, 52, 44, 32, 50, 52, 56, 44, 32, 50, 51, 53, 44, 32, 53, 51, 44, 32, 55, 44, 32, 50, 48, 52, 44, 32, 49, 50, 52, 44, 32, 49, 48, 49, 44, 32, 53, 52, 44, 32, 50, 51, 48, 44, 32, 50, 50, 53, 44, 32, 50, 53, 44, 32, 50, 48, 49, 44, 32, 52, 55, 44, 32, 49, 53, 57, 44, 32, 53, 50, 93]
但是我想将确切的字符串类型转换为字节。所以字节也应该像这样:
[132, 40, 243, 14, 49, 231, 64, 130, 236, 226, 5, 180, 166, 50, 2, 49, 124, 248, 235, 53, 7, 204, 124, 101, 54, 230, 225, 25, 201, 47, 159, 52]
有什么建议吗?
您需要在某处将字符串反序列化为字节。越早越好,因为字节占用的空间比字符串少得多,但您可以根据自己的需要来决定。
您还需要弄清楚输入的格式。我假设这个答案是JSON。下面使用serde_json
将字符串反序列化为Vec<u8>
。(游乐场)
// Replace this with wherever your input comes from.
let input = "[132, 40, 243, 14, 49, 231, 64, 130, 236, 226, 5, 180, 166, 50, 2, 49, 124, 248, 235, 53, 7, 204, 124, 101, 54, 230, 225, 25, 201, 47, 159, 52]";
let bytes: Vec<u8> = serde_json::from_str(input).unwrap();
stream.write_all(&bytes).await.unwrap();