简体   繁体   English

如何将 44 字节长的 base64 字符串(公钥)转换为 32 字节长的 UInt8 数组?

[英]How do I convert a 44 bytes long base64 string (public key) to a 32 bytes long UInt8 array?

I'm using the swift-sodium library and need a 32 bytes long UInt8 array (public key) to seal a message.我正在使用 swift-sodium 库,需要一个 32 字节长的 UInt8 数组(公钥)来密封消息。 However, the public key which is generated by the tweetnacl-js library that I got from an api is a 44 bytes long base64 string.然而,由我从 api 获得的 tweetnacl-js 库生成的公钥是一个 44 字节长的 base64 字符串。 How do I convert the 44 bytes long base64 public key to a 32 bytes long UInt8 array so I can pass it to the seal function?如何将 44 字节长的 base64 公钥转换为 32 字节长的 UInt8 数组,以便将其传递给密封件 function?

You can convert your Base64-String using the following example:您可以使用以下示例转换 Base64 字符串:

extension Data {
   public var bytes: [UInt8]{
      return [UInt8](self)
   }
}

// USAGE IN CODE
let data = Data(base64Encoded: <YOUR BASE64 PUBLIC KEY>, options: .ignoreUnknownCharacters)
print(data?.bytes)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM