Browse Source

integerWithBytes as generic function

Marcin Krzyżanowski 11 years ago
parent
commit
9418b397b7

+ 1 - 15
CryptoSwift/IntExtension.swift

@@ -57,21 +57,7 @@ extension Int {
 
     /** Int with array bytes (little-endian) */
     public static func withBytes(bytes: [Byte]) -> Int {
-        var i:Int = 0
-        var totalBytes = Swift.min(bytes.count, sizeof(Int))
-        
-        // get slice of Int
-        var start = Swift.max(bytes.count - sizeof(Int),0)
-        var intarr = Array<Byte>(bytes[start..<(start + totalBytes)])
-        
-        // extend to Int size if necessary
-        while (intarr.count < sizeof(Int)) {
-            intarr.insert(0 as Byte, atIndex: 0)
-        }
-        
-        var data = NSData(bytes: intarr, length: intarr.count)
-        data.getBytes(&i, length: sizeof(Int));
-        return i.byteSwapped
+        return integerWithBytes(bytes)
     }
 }
 

+ 16 - 58
CryptoSwift/Playground/MyPlayground.playground/section-1.swift

@@ -2,65 +2,23 @@
 
 import Foundation
 
-extension Byte {
-    mutating func shiftRight(count: Byte) -> Byte {
-        if (self == 0) {
-            return self;
-        }
-
-        var bitsCount = Byte(sizeof(Byte) * 8)
-
-        if (count >= bitsCount) {
-            return 0
-        }
-
-        var maxBitsForValue = Byte(floor(log2(Double(self) + 1)))
-        var shiftCount = Swift.min(count, maxBitsForValue - 1)
-        var shiftedValue:Byte = 0;
-        
-        for bitIdx in 0..<bitsCount {
-            var byte = 1 << bitIdx
-            if ((self & byte) == byte) {
-                shiftedValue = shiftedValue | (byte >> shiftCount)
-            }
-        }
-        self = shiftedValue
-        return self
+func withBytes<T: UnsignedIntegerType>(bytes: [Byte]) -> T {
+    var totalBytes = Swift.min(bytes.count, sizeof(T))
+    // get slice of Int
+    var start = Swift.max(bytes.count - sizeof(T),0)
+    var intarr = [Byte](bytes[start..<(start + totalBytes)])
+    
+    // pad size if necessary
+    while (intarr.count < sizeof(T)) {
+        intarr.insert(0 as Byte, atIndex: 0)
     }
+    intarr = intarr.reverse()
+    
+    var i:T = 0
+    var data = NSData(bytes: intarr, length: intarr.count)
+    data.getBytes(&i, length: sizeofValue(i));
+    return i
 }
 
-var b:Byte = 255
-b >> 7
-b.shiftRight(8)
-
-//
-///** Bit operations */
-//extension Int {
-//    /** Shift bits to the right. All bits are shifted (including sign bit) */
-//    private mutating func shiftRight(count: Int) -> Int {
-//        if (self == 0) {
-//            return self;
-//        }
-//        
-//        var bitsCount = sizeof(Int) * 8
-//        var maxBitsForValue = Int(floor(log2(Double(self)) + 1)) - 1
-//        var shiftCount = Swift.min(count, maxBitsForValue)
-//        var shiftedValue:Int = 0;
-//        
-//        for bitIdx in 0..<bitsCount {
-//            // if bit is set then copy to result and shift left 1
-//            var bit = 1 << bitIdx
-//            if ((self & bit) == bit) {
-//                shiftedValue = shiftedValue | (bit >> shiftCount)
-//            }
-//        }
-//        self = Int(shiftedValue)
-//        return self
-//    }
-//}
-//
-//var a:Int = 9223372036854775807
-//a >> 63
-//a.shiftRight(63)
-
+let a:UInt32 = withBytes([0x01,0x01,0x01])
 

+ 3 - 3
CryptoSwift/Playground/MyPlayground.playground/timeline.xctimeline

@@ -3,13 +3,13 @@
    version = "3.0">
    <TimelineItems>
       <LoggerValueHistoryTimelineItem
-         documentLocation = "file:///Users/marcinkrzyzanowski/Devel/CryptoSwift/CryptoSwift/Playground/MyPlayground.playground#CharacterRangeLen=0&amp;CharacterRangeLoc=792&amp;EndingLineNumber=3&amp;StartingLineNumber=3&amp;Timestamp=431360683.801244">
+         documentLocation = "file:///Users/marcinkrzyzanowski/Devel/CryptoSwift/CryptoSwift/Playground/MyPlayground.playground#CharacterRangeLen=0&amp;CharacterRangeLoc=72&amp;EndingLineNumber=3&amp;StartingLineNumber=3&amp;Timestamp=431362043.187306">
       </LoggerValueHistoryTimelineItem>
       <LoggerValueHistoryTimelineItem
-         documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=792&amp;EndingColumnNumber=7&amp;EndingLineNumber=3&amp;StartingColumnNumber=5&amp;StartingLineNumber=3&amp;Timestamp=431360683.801244">
+         documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=72&amp;EndingColumnNumber=7&amp;EndingLineNumber=3&amp;StartingColumnNumber=5&amp;StartingLineNumber=3&amp;Timestamp=431362043.187547">
       </LoggerValueHistoryTimelineItem>
       <LoggerValueHistoryTimelineItem
-         documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=792&amp;EndingColumnNumber=20&amp;EndingLineNumber=51&amp;StartingColumnNumber=17&amp;StartingLineNumber=51&amp;Timestamp=431360683.801244">
+         documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=72&amp;EndingColumnNumber=20&amp;EndingLineNumber=4&amp;StartingColumnNumber=17&amp;StartingLineNumber=4&amp;Timestamp=431362043.187763">
       </LoggerValueHistoryTimelineItem>
    </TimelineItems>
 </Timeline>

+ 1 - 15
CryptoSwift/UInt32Extension.swift

@@ -20,21 +20,7 @@ extension UInt32 {
 
     /** Int with array bytes (little-endian) */
     public static func withBytes(bytes: [Byte]) -> UInt32 {
-        var i:UInt32 = 0
-        var totalBytes = Swift.min(bytes.count, sizeofValue(i))
-        
-        // get slice of Int
-        var start = Swift.max(bytes.count - sizeofValue(i),0)
-        var intarr = [Byte](bytes[start..<(start + totalBytes)])
-        
-        // extend to Int size if necessary
-        while (intarr.count < sizeofValue(i)) {
-            intarr.insert(0 as Byte, atIndex: 0)
-        }
-        
-        var data = NSData(bytes: intarr, length: intarr.count)
-        data.getBytes(&i, length: sizeofValue(i));
-        return i.byteSwapped
+        return integerWithBytes(bytes)
     }
 }
 

+ 19 - 0
CryptoSwift/Utils.swift

@@ -34,6 +34,25 @@ func reverseBytes(value: UInt32) -> UInt32 {
     return tmp1 | tmp2
 }
 
+// MARK: Generics
+
+func integerWithBytes<T: IntegerType>(bytes: [Byte]) -> T {
+    var totalBytes = Swift.min(bytes.count, sizeof(T))
+    // get slice of Int
+    var start = Swift.max(bytes.count - sizeof(T),0)
+    var intarr = [Byte](bytes[start..<(start + totalBytes)])
+    
+    // pad size if necessary
+    while (intarr.count < sizeof(T)) {
+        intarr.insert(0 as Byte, atIndex: 0)
+    }
+    intarr = intarr.reverse()
+    
+    var i:T = 0
+    var data = NSData(bytes: intarr, length: intarr.count)
+    data.getBytes(&i, length: sizeofValue(i));
+    return i
+}
 
 /** array of bytes, little-endian representation */
 func bytesArray<T>(value:T, totalBytes:Int) -> [Byte] {

+ 3 - 3
CryptoSwiftTests/CipherTests.swift

@@ -48,9 +48,9 @@ class CipherTests: XCTestCase {
                                  0x2a,0x7d,0xfb,0x4b,0x3d,0x33,0x05,0xd9]
         
         
-        let poly = Poly1305(key: key);
-        let mac:[Byte] = [Byte](count: 16, repeatedValue: 0);
-        poly.auth(mac, m: msg)
+//        let poly = Poly1305(key: key);
+//        let mac:[Byte] = [Byte](count: 16, repeatedValue: 0);
+//        poly.auth(mac, m: msg)
     }
 
     func testChaCha20() {