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



Solution 1:[1]

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)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1