简体   繁体   中英

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. 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. 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?

You can convert your Base64-String using the following example:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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