Replies: 10 comments
-
The problem is, If i have a Base64 String I would need to decode it to verify whether it matches with the expected password. Later on I am using that decoded string as Byte Array. If there is a way for directly getting in byte array format, it would work |
Beta Was this translation helpful? Give feedback.
-
to get back from base64 string to bytes you may use Foundation. |
Beta Was this translation helpful? Give feedback.
-
I am getting different Byte Array in this case. My code crashes in the next step. |
Beta Was this translation helpful? Give feedback.
-
if let nsdata = NSData(base64EncodedString: hashedValue, options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters) {
var decodedHashedPassword = [UInt8](count: nsdata.length, repeatedValue: 0)
nsdata.getBytes(&decodedHashedPassword)
print(decodedHashedPassword) The "decodedHashedPassword" doesn't give me the exact byte array before the encoding. Whereas there are around 5 extra elements added in start of the array. |
Beta Was this translation helpful? Give feedback.
-
what line should I look at ? |
Beta Was this translation helpful? Give feedback.
-
try narrow it to 4 lines of code, then verify your input and output. Get aray of bytes, encode it to base64, then decode to array. ps. It's not really related to CryptoSwift, is it ? |
Beta Was this translation helpful? Give feedback.
-
Edited the previous comment. |
Beta Was this translation helpful? Give feedback.
-
I figured that out. The issue was with the NSDataBase64DecodingOptions. NSDataBase64DecodingOptions([]) worked for me. Now, i am facing other issue with shifting bits. I saw an extension about that in CryptoSwift. I tried to use it "&>>" but it gives me unrecognized operator. |
Beta Was this translation helpful? Give feedback.
-
@shorabhd This might help you: //1. Convert String to base64
//Convert string to NSData
let myData = myString.dataUsingEncoding(NSUTF8StringEncoding)!
//Encode to base64
let myBase64Data = myData.base64EncodedDataWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)
//Decode base64
let resultData = NSData(base64EncodedData: myBase64Data, options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters)!
//Convert NSData to NSString
let resultNSString = NSString(data: resultData, encoding: NSUTF8StringEncoding)!
//Convert NSString to String
let resultString = resultNSString as String
print(resultString)
//2. Convert [UInt8] to base64
//Convert string to [UInt8]
let array : [UInt8] = Array(myString.utf8)
//Convert [UInt8] to NSData
let data = NSData(bytes: array, length: array.count)
//Encode to base64
let base64Data = data.base64EncodedDataWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)
//Decode base64
let newData = NSData(base64EncodedData: base64Data, options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters)!
//Convert NSData to NSString
let newNSString = NSString(data: newData, encoding: NSUTF8StringEncoding)!
print(newNSString) |
Beta Was this translation helpful? Give feedback.
-
@shorabhd All I can do about it is pointing you to this blog post: https://medium.com/@krzyzanowskim/swiftly-shift-bits-and-protect-yourself-be33016ce071 and these tests: https://github.com/krzyzanowskim/CryptoSwift/blob/master/CryptoSwiftTests/ExtensionsTest.swift#L58 |
Beta Was this translation helpful? Give feedback.
-
Hey,
I am stuck at decoding the encrypted Base64 using toBase64() method. I want to get it back to Array so that i can verify the password. Please let me know.
Beta Was this translation helpful? Give feedback.
All reactions