|
@@ -60,33 +60,50 @@ extension String {
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
- private static var unicodeHexMap:[UnicodeScalar:UInt8] = ["0":0,"1":1,"2":2,"3":3,"4":4,"5":5,"6":6,"7":7,"8":8,"9":9,"a":10,"b":11,"c":12,"d":13,"e":14,"f":15]
|
|
|
|
|
|
+ private static var unicodeHexMap:[UnicodeScalar:UInt8] = ["0":0,"1":1,"2":2,"3":3,"4":4,"5":5,"6":6,"7":7,"8":8,"9":9,"a":10,"b":11,"c":12,"d":13,"e":14,"f":15,"A":10,"B":11,"C":12,"D":13,"E":14,"F":15]
|
|
/**
|
|
/**
|
|
Converts the a hexadecimal string to a corresponding array of bytes.
|
|
Converts the a hexadecimal string to a corresponding array of bytes.
|
|
|
|
|
|
So the string "ff00" would get converted to [255, 0].
|
|
So the string "ff00" would get converted to [255, 0].
|
|
|
|
+ Supports odd number of characters by interpreting the last character as its' hex value ("f" produces [16] as if it was "0f").
|
|
|
|
|
|
- Returns: An array of 8 bit unsigned integers (bytes).
|
|
- Returns: An array of 8 bit unsigned integers (bytes).
|
|
*/
|
|
*/
|
|
- public func hexToBytes() -> [UInt8]{
|
|
|
|
- var bytes:[UInt8] = []
|
|
|
|
|
|
+ public func hexToBytes() -> Array<UInt8>{
|
|
|
|
+ var bytes:Array<UInt8> = []
|
|
|
|
+ do{
|
|
|
|
+ try self.streamHexBytes { (byte) in
|
|
|
|
+ bytes.append(byte)
|
|
|
|
+ }
|
|
|
|
+ }catch _ as NSError{
|
|
|
|
+ return []
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return bytes
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // Allows str.hexToBytes() and Array<UInt8>(hex: str) to share the same code
|
|
|
|
+ // Old functionality returns a blank array upon encountering a non hex character so this method must throw in order to replicate that in methods relying on this method. (ex. "ffn" should return [] instead of [255])
|
|
|
|
+ // Consider switching NSError to custom Error enum
|
|
|
|
+ public func streamHexBytes(_ block:(UInt8)->Void) throws{
|
|
var buffer:UInt8?
|
|
var buffer:UInt8?
|
|
- var skip = self.hasPrefix("0x") ? 2 : 0
|
|
|
|
|
|
+ var skip = hasPrefix("0x") ? 2 : 0
|
|
for char in unicodeScalars.lazy {
|
|
for char in unicodeScalars.lazy {
|
|
- guard skip == 0 else {skip -= 1;continue}
|
|
|
|
- guard let value = String.unicodeHexMap[char] else {return []}
|
|
|
|
|
|
+ guard skip == 0 else {
|
|
|
|
+ skip -= 1
|
|
|
|
+ continue
|
|
|
|
+ }
|
|
|
|
+ guard let value = String.unicodeHexMap[char] else {
|
|
|
|
+ throw NSError(domain: "com.krzyzanowskim.CryptoSwift.hexParseError", code: 0, userInfo: nil)
|
|
|
|
+ }
|
|
if let b = buffer {
|
|
if let b = buffer {
|
|
- let byte = b << 4 | value
|
|
|
|
- bytes.append(byte)
|
|
|
|
|
|
+ block(b << 4 | value)
|
|
buffer = nil
|
|
buffer = nil
|
|
} else {
|
|
} else {
|
|
buffer = value
|
|
buffer = value
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- if let b = buffer{
|
|
|
|
- bytes.append(b)
|
|
|
|
- }
|
|
|
|
- return bytes
|
|
|
|
|
|
+ if let b = buffer{block(b)}
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|