Browse Source

Define toHexString on Array<UInt8>

Marcin Krzyżanowski 10 years ago
parent
commit
0b842b61eb
2 changed files with 17 additions and 9 deletions
  1. 16 0
      CryptoSwift/ArrayExtension.swift
  2. 1 9
      CryptoSwift/NSDataExtension.swift

+ 16 - 0
CryptoSwift/ArrayExtension.swift

@@ -25,3 +25,19 @@ extension Array {
         return words
         return words
     }
     }
 }
 }
+
+// MARK: - Array<UInt8>
+
+public protocol _UInt8Type {}
+extension UInt8: _UInt8Type {}
+
+extension Array where Element: _UInt8Type {
+    public func toHexString() -> String {
+        var s:String = "";
+        for byte in self {
+            s = s + String(format:"%02x", byte as! UInt8)
+        }
+        return s
+    }
+}
+

+ 1 - 9
CryptoSwift/NSDataExtension.swift

@@ -86,15 +86,7 @@ extension NSData {
 extension NSData {
 extension NSData {
     
     
     public func toHexString() -> String {
     public func toHexString() -> String {
-        let count = self.length / sizeof(UInt8)
-        var bytesArray = [UInt8](count: count, repeatedValue: 0)
-        self.getBytes(&bytesArray, length:count * sizeof(UInt8))
-        
-        var s:String = "";
-        for byte in bytesArray {
-            s = s + String(format:"%02x", byte)
-        }
-        return s
+        return self.arrayOfBytes().toHexString()
     }
     }
     
     
     public func arrayOfBytes() -> [UInt8] {
     public func arrayOfBytes() -> [UInt8] {