0

I have been trying to implement Coinbase API in my application that is built using swift 5 and xcode. Whenever I try to convert this base64 encoded data to Normal string, it returns nil.

Here is the string

fI5GtCKFF+O8j8uJlJVGxIvolVUUrVMT9GBouxlpEOUXmaGbwQvHt0z8kK0fUwQaKa45KUOLWHP/CQaM70bhdQ==

Below are the answers i've tried

extension String {

func base64Encoded() -> String? {
    return data(using: .utf8)?.base64EncodedString()
}

func base64Decoded() -> String? {
    var st = self;
    print(st.count)
    let remainder = self.count % 4
    if remainder > 0 {
        st = self.padding(toLength: self.count + 4 - remainder,
                                      withPad: "=",
                                      startingAt: 0)
    }

    print(st.count)
    guard let d = Data(base64Encoded: st, options: .ignoreUnknownCharacters) else{
        return nil
    }
    return String(data: d, encoding: .utf8)
}   
}

THE DIFFERENCE

let base64Decoded = Data(base64Encoded: "fI5GtCKFF+O8j8uJlJVGxIvolVUUrVMT9GBouxlpEOUXmaGbwQvHt0z8kK0fUwQaKa45KUOLWHP/CQaM70bhdQ==")!
let sign = HMAC.sign(data: "1".toData()!, algorithm: .sha256, key: base64Decoded)
let signData = sign.base64EncodedString()

The result i receive: vBZ+Z63rFqNmU61yrd91PGQB8f1iqocWkLlAev5XJOc=

It's different from: https://www.liavaag.org/English/SHA-Generator/HMAC/

Link to result There's is the correct output. What am i missing or doing wrong ?

  • Who said that it's UTF8 encoding? I got result, but not with UTF8 encoding. With ASCII, it works for instance. – Larme Mar 30 '21 at 15:29
  • use https://www.base64decode.org/ to check your base64 string, it does not seem valid. – Shawn Frank Mar 30 '21 at 15:30
  • @Larme I took a look at the result using .ASCII ... doesn't seem accurate. – ineedtolearntoeat Mar 30 '21 at 15:45
  • 2
    What's supposed to be the value? Because I checked an online tool, and it seemed to be the value. – Larme Mar 30 '21 at 15:46
  • That string will encode using `.init(base64encoded:)` which suggests that it is valid as it's a failable initialiser that return nil if not a valid b64 string. – flanker Mar 30 '21 at 15:49
  • Base64 is a format for encoding arbitrary data. It would be somewhat unusual for it to be used to encode a simple string (sometimes it's used that way, but it's inefficient). What specific string were you expecting? Why are you expecting a string at all? (Also, this is a normal Base64 encoding. There's no reason for the extra padding code or `.ignoreUnknownCharacters`. You can and should just use `Data(base64Encoded:)` directly.) – Rob Napier Mar 30 '21 at 15:59
  • @RobNapier the result i get in swift and php for the same input is different – ineedtolearntoeat Mar 31 '21 at 17:02
  • @Larme I' was given the encoded version first. But the result i get when I decode on PHP and swift are two different values – ineedtolearntoeat Mar 31 '21 at 17:03
  • Your Swift and PHP are the same data, they're just corrupted in different ways due to your invalid decoding. The "�" you see in the PHP indicates it tried to decode a UTF-8 codepoint and got an error. (In Swift, that returns nil instead of substituting the replacement character.) This is not a UTF-8 or ASCII string (or any kind of string). To see the actual data, use this from the commandline: `echo ...base64... | base64 -D | xxd`. This will show the raw bytes. This is not text. – Rob Napier Mar 31 '21 at 17:06
  • Where did you get this Base64-encoded data? What do you believe it encodes (and why)? It's 128 bytes long and lacks any obvious structure, which means it's probably either a 1024-bit key of some sort, or several blocks of encrypted data. – Rob Napier Mar 31 '21 at 17:14
  • 1
    Following up @RobNapier comment, you can use https://stackoverflow.com/questions/39075043/how-to-convert-data-to-hex-string-in-swift to show "hex data", and help you compre. Stop thinking that any `Data` is convertible to "UTF8", nor ASCII, etc. – Larme Mar 31 '21 at 18:12
  • @Larme I've updated the code but my results differ from the generator I used above. Please take a look at the new updated edit – ineedtolearntoeat Mar 31 '21 at 21:27
  • @RobNapier Please take a look at the "difference" in the new edit – ineedtolearntoeat Mar 31 '21 at 21:28
  • "THE DIFFERENCE" seems to be a completely unrelated question. Which is your actual question? And what is your code for `HMAC.sign(data:algorithm:key:)`? I don't believe that's a built-in function. If it's something custom you've written, then it could do things differently than the website you've linked. (I also don't see source code for that, so I don't know precisely what it's doing, and if it computes this correctly.) What is your goal? – Rob Napier Mar 31 '21 at 23:32
  • Also `.toData()` is custom code, so we need to know what it does. See https://stackoverflow.com/help/minimal-reproducible-example for what you need to provide here. – Rob Napier Mar 31 '21 at 23:46
  • @RobNapier Got it to work / match! Thank you!! – ineedtolearntoeat Apr 01 '21 at 00:25

0 Answers0