Selaa lähdekoodia

Merge pull request #172 from richlowenberg/master

Added seed ability to crc
Marcin Krzyzanowski 9 vuotta sitten
vanhempi
commit
2380fc17ed

+ 4 - 4
CryptoSwift/CRC.swift

@@ -74,8 +74,8 @@ final class CRC {
         0x4400, 0x84C1, 0x8581, 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641,
         0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040]
     
-    func crc32(message:[UInt8]) -> UInt32 {
-        var crc:UInt32 = 0xffffffff
+    func crc32(message:[UInt8], seed: UInt32? = nil) -> UInt32 {
+        var crc:UInt32 = seed != nil ? seed! : 0xffffffff
         
         for chunk in BytesSequence(chunkSize: 256, data: message) {
             for b in chunk {
@@ -86,8 +86,8 @@ final class CRC {
         return crc ^ 0xffffffff
     }
     
-    func crc16(message:[UInt8]) -> UInt16 {
-        var crc:UInt16 = 0x0000
+    func crc16(message:[UInt8], seed: UInt16? = nil) -> UInt16 {
+        var crc:UInt16 = seed != nil ? seed! : 0x0000
         for chunk in BytesSequence(chunkSize: 256, data: message) {
             for b in chunk {
                 crc = (crc >> 8) ^ CRC.table16[Int((crc ^ UInt16(b)) & 0xFF)]

+ 4 - 4
CryptoSwift/CSArrayType+Extensions.swift

@@ -46,12 +46,12 @@ public extension CSArrayType where Generator.Element == UInt8 {
         return Hash.sha512(cs_arrayValue()).calculate()
     }
     
-    public func crc32() -> [Generator.Element] {
-        return Hash.crc32(cs_arrayValue()).calculate()
+    public func crc32(seed: UInt32? = nil) -> [Generator.Element] {
+        return Hash.crc32(cs_arrayValue(), seed: seed).calculate()
     }
     
-    public func crc16() -> [Generator.Element] {
-        return Hash.crc16(cs_arrayValue()).calculate()
+    public func crc16(seed: UInt16? = nil) -> [Generator.Element] {
+        return Hash.crc16(cs_arrayValue(), seed: seed).calculate()
     }
     
     public func encrypt(cipher: Cipher) throws -> [Generator.Element] {

+ 4 - 4
CryptoSwift/Foundation/NSData+Extension.swift

@@ -60,13 +60,13 @@ extension NSData {
         return NSData.withBytes(result)
     }
 
-    public func crc32() -> NSData? {
-        let result = Hash.crc32(self.arrayOfBytes()).calculate()
+    public func crc32(seed: UInt32? = nil) -> NSData? {
+        let result = Hash.crc32(self.arrayOfBytes(), seed: seed).calculate()
         return NSData.withBytes(result)
     }
 
-    public func crc16() -> NSData? {
-        let result = Hash.crc16(self.arrayOfBytes()).calculate()
+    public func crc16(seed: UInt16? = nil) -> NSData? {
+        let result = Hash.crc16(self.arrayOfBytes(), seed: seed).calculate()
         return NSData.withBytes(result)
     }
 

+ 4 - 4
CryptoSwift/Hash.swift

@@ -10,8 +10,8 @@ public enum Hash {
     case md5(Array<UInt8>)
     case sha1(Array<UInt8>)
     case sha224(Array<UInt8>), sha256(Array<UInt8>), sha384(Array<UInt8>), sha512(Array<UInt8>)
-    case crc32(Array<UInt8>)
-    case crc16(Array<UInt8>)
+    case crc32(Array<UInt8>, seed: UInt32?)
+    case crc16(Array<UInt8>, seed: UInt16?)
     
     public func calculate() -> [UInt8] {
         switch self {
@@ -28,9 +28,9 @@ public enum Hash {
         case sha512(let bytes):
             return SHA2(bytes, variant: .sha512).calculate64()
         case crc32(let bytes):
-            return CRC().crc32(bytes).bytes()
+            return CRC().crc32(bytes.0, seed: bytes.seed).bytes()
         case crc16(let bytes):
-            return UInt32(CRC().crc16(bytes)).bytes(2)
+            return UInt32(CRC().crc16(bytes.0, seed: bytes.seed)).bytes(2)
         }
     }
 }

+ 4 - 4
CryptoSwift/String+Extension.swift

@@ -33,12 +33,12 @@ extension String {
         return self.utf8.lazy.map({ $0 as UInt8 }).sha512().toHexString()
     }
 
-    public func crc32() -> String {
-        return self.utf8.lazy.map({ $0 as UInt8 }).crc32().toHexString()
+    public func crc32(seed: UInt32? = nil) -> String {
+        return self.utf8.lazy.map({ $0 as UInt8 }).crc32(seed).toHexString()
     }
 
-    public func crc16() -> String {
-        return self.utf8.lazy.map({ $0 as UInt8 }).crc16().toHexString()
+    public func crc16(seed: UInt16? = nil) -> String {
+        return self.utf8.lazy.map({ $0 as UInt8 }).crc16(seed).toHexString()
     }
 
     public func encrypt(cipher: Cipher) throws -> [UInt8] {

+ 2 - 2
CryptoSwiftTests/HashTests.swift

@@ -119,11 +119,11 @@ final class CryptoSwiftTests: XCTestCase {
     
     func testCRC32() {
         let data:NSData = NSData(bytes: [49, 50, 51] as [UInt8], length: 3)
-        if let crc = data.crc32() {
+        if let crc = data.crc32(nil) {
             XCTAssertEqual(crc.toHexString(), "884863d2", "CRC32 calculation failed");
         }
         
-        XCTAssertEqual("".crc32(), "00000000", "CRC32 calculation failed");
+        XCTAssertEqual("".crc32(nil), "00000000", "CRC32 calculation failed");
     }
     
     func testCRC16() {