Browse Source

More updates to API

Marcin Krzyżanowski 9 years ago
parent
commit
cf0116abc1

+ 2 - 2
CryptoSwiftTests/AESTests.swift

@@ -216,7 +216,7 @@ final class AESTests: XCTestCase {
         let message = Array<UInt8>(repeating: 7, count: 1024 * 1024)
         let aes = try! AES(key: key, iv: iv, blockMode: .CBC, padding: PKCS7())
         measureMetrics([XCTPerformanceMetric_WallClockTime], automaticallyStartMeasuring: true, for: { () -> Void in
-            try! aes.encrypt(bytes: message)
+            _ = try! aes.encrypt(bytes: message)
         })
     }
 
@@ -226,7 +226,7 @@ final class AESTests: XCTestCase {
         let message = Array<UInt8>(repeating: 7, count: 1024 * 1024)
         let aes = try! AES(key: key, iv: iv, blockMode: .CBC, padding: PKCS7())
         measureMetrics([XCTPerformanceMetric_WallClockTime], automaticallyStartMeasuring: true, for: { () -> Void in
-            try! aes.decrypt(bytes: message)
+            _ = try! aes.decrypt(bytes: message)
         })
     }
 

+ 6 - 6
CryptoSwiftTests/PaddingTests.swift

@@ -12,27 +12,27 @@ final class PaddingTests: XCTestCase {
     func testPKCS7_0() {
         let input:Array<UInt8>    = [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6]
         let expected:Array<UInt8> = [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16]
-        let padded = PKCS7().add(data: input, blockSize: 16)
+        let padded = PKCS7().add(to: input, blockSize: 16)
         XCTAssertEqual(padded, expected, "PKCS7 failed")
-        let clean = PKCS7().remove(data: padded, blockSize: nil)
+        let clean = PKCS7().remove(from: padded, blockSize: nil)
         XCTAssertEqual(clean, input, "PKCS7 failed")
     }
     
     func testPKCS7_1() {
         let input:Array<UInt8>    = [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5]
         let expected:Array<UInt8> = [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,1]
-        let padded = PKCS7().add(data: input, blockSize: 16)
+        let padded = PKCS7().add(to: input, blockSize: 16)
         XCTAssertEqual(padded, expected, "PKCS7 failed")
-        let clean = PKCS7().remove(data: padded, blockSize: nil)
+        let clean = PKCS7().remove(from: padded, blockSize: nil)
         XCTAssertEqual(clean, input, "PKCS7 failed")
     }
     
     func testPKCS7_2() {
         let input:Array<UInt8>    = [1,2,3,4,5,6,7,8,9,0,1,2,3,4]
         let expected:Array<UInt8> = [1,2,3,4,5,6,7,8,9,0,1,2,3,4,2,2]
-        let padded = PKCS7().add(data: input, blockSize: 16)
+        let padded = PKCS7().add(to: input, blockSize: 16)
         XCTAssertEqual(padded, expected, "PKCS7 failed")
-        let clean = PKCS7().remove(data: padded, blockSize: nil)
+        let clean = PKCS7().remove(from: padded, blockSize: nil)
         XCTAssertEqual(clean, input, "PKCS7 failed")
     }
 }

+ 1 - 1
CryptoSwiftTests/Poly1305Tests.swift

@@ -29,7 +29,7 @@ final class Poly1305Tests: XCTestCase {
         
         // extensions
         let msgData = NSData.with(bytes: msg)
-        let mac2 = try! msgData.authenticate(authenticator: Authenticator.Poly1305(key: key))
+        let mac2 = try! msgData.authenticate(with: Authenticator.Poly1305(key: key))
         XCTAssertEqual(mac2, NSData.with(bytes: expectedMac), "Invalid authentication result")
     }
 }

+ 4 - 4
Sources/CryptoSwift/AES.swift

@@ -326,7 +326,7 @@ extension AES {
                 tmp[wordIdx] = w[4*(i-1)+wordIdx]
             }
             if ((i % variant.Nk) == 0) {
-                tmp = subWord(word: rotateLeft(v: UInt32.with(bytes: tmp), 8).bytes(totalBytes: sizeof(UInt32)))
+                tmp = subWord(word: rotateLeft(UInt32.with(bytes: tmp), by: 8).bytes(totalBytes: sizeof(UInt32)))
                 tmp[0] = tmp.first! ^ Rcon[i/variant.Nk]
             } else if (variant.Nk > 6 && (i % variant.Nk) == 4) {
                 tmp = subWord(word: tmp)
@@ -379,7 +379,7 @@ extension AES {
             q ^= q << 4
             q ^= (q & 0x80) == 0x80 ? 0x09 : 0
             
-            let s = 0x63 ^ q ^ rotateLeft(v: q, 1) ^ rotateLeft(v: q, 2) ^ rotateLeft(v: q, 3) ^ rotateLeft(v: q, 4)
+            let s = 0x63 ^ q ^ rotateLeft(q, by: 1) ^ rotateLeft(q, by: 2) ^ rotateLeft(q, by: 3) ^ rotateLeft(q, by: 4)
             
             sbox[Int(p)] = UInt32(s)
             invsbox[Int(s)] = UInt32(p)
@@ -407,7 +407,7 @@ extension AES {
             self.accumulated += bytes
 
             if (isLast) {
-                self.accumulated = padding.add(data: self.accumulated, blockSize: AES.blockSize)
+                self.accumulated = padding.add(to: self.accumulated, blockSize: AES.blockSize)
             }
 
             //CTR does not require full block therefore work with anything
@@ -459,7 +459,7 @@ extension AES {
             }
 
             if (isLast) {
-                plaintext = padding.remove(data: plaintext, blockSize: AES.blockSize)
+                plaintext = padding.remove(from: plaintext, blockSize: AES.blockSize)
             }
 
             return plaintext

+ 3 - 17
Sources/CryptoSwift/Array+Extension.swift

@@ -13,27 +13,13 @@ extension Array {
         var words = Array<Array<Element>>()
         words.reserveCapacity(self.count / chunksize)
         for idx in stride(from: chunksize, through: self.count, by: chunksize) {
-            let word = Array(self[idx - chunksize..<idx]) // this is slow for large table
-            words.append(word)
+            words.append(Array(self[idx - chunksize..<idx])) // slow for large table
         }
-        let reminder = Array(self.suffix(self.count % chunksize))
+        let reminder = self.suffix(self.count % chunksize)
         if !reminder.isEmpty {
-            words.append(reminder)
+            words.append(Array(reminder))
         }
         return words
     }
-    
-    /*
-    This helper call is slow, therefore don't use it. It is due to extension, or due to optimization that can be done
-    
-    subscript(index: UInt32) -> Element {
-        get {
-            return self[Int(index)]
-        }
-        set {
-            self[Int(index)] = newValue
-        }
-    }
-    */
 }
 

+ 3 - 3
Sources/CryptoSwift/Authenticator.swift

@@ -28,15 +28,15 @@ public enum Authenticator {
     
     - returns: 16-byte message authentication code
     */
-    public func authenticate(message: Array<UInt8>) throws -> Array<UInt8> {
+    public func authenticate(bytes: Array<UInt8>) throws -> Array<UInt8> {
         switch (self) {
         case .Poly1305(let key):
-            guard let auth = CryptoSwift.Poly1305(key: key)?.authenticate(message: message) else {
+            guard let auth = CryptoSwift.Poly1305(key: key)?.authenticate(bytes: bytes) else {
                 throw Error.AuthenticateError
             }
             return auth
         case .HMAC(let key, let variant):
-            guard let auth = CryptoSwift.HMAC(key: key, variant: variant)?.authenticate(message: message) else {
+            guard let auth = CryptoSwift.HMAC(key: key, variant: variant)?.authenticate(bytes: bytes) else {
                 throw Error.AuthenticateError
             }
             return auth

+ 2 - 2
Sources/CryptoSwift/CRC.swift

@@ -78,11 +78,11 @@ final class CRC {
         var crc:UInt32 = seed != nil ? seed! : 0xffffffff
         for chunk in BytesSequence(chunkSize: 256, data: message) {
             for b in chunk {
-                let idx = Int((crc ^ UInt32(reflect ? b : reverseUInt8(uint8: b))) & 0xff)
+                let idx = Int((crc ^ UInt32(reflect ? b : reverse(uint8: b))) & 0xff)
                 crc = (crc >> 8) ^ CRC.table32[idx]
             }
         }
-        return (reflect ? crc : reverseUInt32(uint32: crc)) ^ 0xffffffff
+        return (reflect ? crc : reverse(uint32: crc)) ^ 0xffffffff
     }
     
     func crc16(message:Array<UInt8>, seed: UInt16? = nil) -> UInt16 {

+ 2 - 2
Sources/CryptoSwift/CSArrayType+Extensions.swift

@@ -63,7 +63,7 @@ public extension CSArrayType where Iterator.Element == UInt8 {
         return try cipher.decrypt(bytes: cs_arrayValue())
     }
     
-    public func authenticate(authenticator: Authenticator) throws -> [Iterator.Element] {
-        return try authenticator.authenticate(message: cs_arrayValue())
+    public func authenticate(with authenticator: Authenticator) throws -> [Iterator.Element] {
+        return try authenticator.authenticate(bytes: cs_arrayValue())
     }
 }

+ 4 - 4
Sources/CryptoSwift/ChaCha20.swift

@@ -149,16 +149,16 @@ final public class ChaCha20: BlockCipher {
     
     private final func quarterround(a:inout UInt32, _ b:inout UInt32, _ c:inout UInt32, _ d:inout UInt32) {
         a = a &+ b
-        d = rotateLeft(v: (d ^ a), 16) //FIXME: WAT? n:
+        d = rotateLeft((d ^ a), by: 16) //FIXME: WAT? n:
         
         c = c &+ d
-        b = rotateLeft(v: (b ^ c), 12);
+        b = rotateLeft((b ^ c), by: 12);
         
         a = a &+ b
-        d = rotateLeft(v: (d ^ a), 8);
+        d = rotateLeft((d ^ a), by: 8);
 
         c = c &+ d
-        b = rotateLeft(v: (b ^ c), 7);
+        b = rotateLeft((b ^ c), by: 7);
     }
 }
 

+ 2 - 2
Sources/CryptoSwift/Foundation/NSData+Extension.swift

@@ -80,8 +80,8 @@ extension NSData {
         return NSData.with(bytes: decrypted)
     }
     
-    public func authenticate(authenticator: Authenticator) throws -> NSData {
-        let result = try authenticator.authenticate(message: self.arrayOfBytes())
+    public func authenticate(with authenticator: Authenticator) throws -> NSData {
+        let result = try authenticator.authenticate(bytes: self.arrayOfBytes())
         return NSData.with(bytes: result)
     }
 }

+ 1 - 1
Sources/CryptoSwift/Generics.swift

@@ -58,7 +58,7 @@ func integerWith<T:Integer where T:ByteConvertible, T: BitshiftOperationsType>(b
 /// I found this method slow
 func arrayOfBytes<T>(value:T, length:Int? = nil) -> Array<UInt8> {
     let totalBytes = length ?? sizeof(T)
-    
+
     let valuePointer = UnsafeMutablePointer<T>(allocatingCapacity: 1)
     valuePointer.pointee = value
     

+ 2 - 2
Sources/CryptoSwift/HMAC.swift

@@ -69,7 +69,7 @@ final public class HMAC {
         }
     }
 
-    public func authenticate(message:Array<UInt8>) -> Array<UInt8>? {
+    public func authenticate(bytes:Array<UInt8>) -> Array<UInt8>? {
         var opad = Array<UInt8>(repeating: 0x5c, count: variant.blockSize())
         for (idx, _) in key.enumerated() {
             opad[idx] = key[idx] ^ opad[idx]
@@ -80,7 +80,7 @@ final public class HMAC {
         }
 
         var finalHash:Array<UInt8>? = nil;
-        if let ipadAndMessageHash = variant.calculateHash(bytes: ipad + message) {
+        if let ipadAndMessageHash = variant.calculateHash(bytes: ipad + bytes) {
             finalHash = variant.calculateHash(bytes: opad + ipadAndMessageHash);
         }
         return finalHash

+ 1 - 1
Sources/CryptoSwift/MD5.swift

@@ -95,7 +95,7 @@ final class MD5 : HashProtocol  {
                 dTemp = D
                 D = C
                 C = B
-                B = B &+ rotateLeft(v: (A &+ F &+ k[j] &+ M[g]), s[j])
+                B = B &+ rotateLeft(A &+ F &+ k[j] &+ M[g], by: s[j])
                 A = dTemp    
             }
             

+ 2 - 2
Sources/CryptoSwift/NoPadding.swift

@@ -11,11 +11,11 @@ public struct NoPadding: Padding {
     
     }
     
-    public func add(data: Array<UInt8>, blockSize:Int) -> Array<UInt8> {
+    public func add(to data: Array<UInt8>, blockSize:Int) -> Array<UInt8> {
         return data;
     }
 
-    public func remove(data: Array<UInt8>, blockSize:Int?) -> Array<UInt8> {
+    public func remove(from data: Array<UInt8>, blockSize:Int?) -> Array<UInt8> {
         return data;
     }
 }

+ 2 - 2
Sources/CryptoSwift/PKCS5/PBKDF2.swift

@@ -81,7 +81,7 @@ private extension PKCS5.PBKDF2 {
     // F (P, S, c, i) = U_1 \xor U_2 \xor ... \xor U_c
     // U_1 = PRF (P, S || INT (i))
     private func calculateBlock(salt: Array<UInt8>, blockNum: UInt) -> Array<UInt8>? {
-        guard let u1 = prf.authenticate(message: salt + INT(i: blockNum)) else {
+        guard let u1 = prf.authenticate(bytes: salt + INT(i: blockNum)) else {
             return nil
         }
 
@@ -91,7 +91,7 @@ private extension PKCS5.PBKDF2 {
             // U_2 = PRF (P, U_1) ,
             // U_c = PRF (P, U_{c-1}) .
             for _ in 2...self.iterations {
-                u = prf.authenticate(message: u)!
+                u = prf.authenticate(bytes: u)!
                 for x in 0..<ret.count {
                     ret[x] = ret[x] ^ u[x]
                 }

+ 2 - 2
Sources/CryptoSwift/PKCS7.swift

@@ -19,7 +19,7 @@ public struct PKCS7: Padding {
         
     }
     
-    public func add(data bytes: Array<UInt8> , blockSize:Int) -> Array<UInt8> {
+    public func add(to bytes: Array<UInt8> , blockSize:Int) -> Array<UInt8> {
         let padding = UInt8(blockSize - (bytes.count % blockSize))
         var withPadding = bytes
         if (padding == 0) {
@@ -36,7 +36,7 @@ public struct PKCS7: Padding {
         return withPadding
     }
 
-    public func remove(data bytes: Array<UInt8>, blockSize:Int?) -> Array<UInt8> {
+    public func remove(from bytes: Array<UInt8>, blockSize:Int?) -> Array<UInt8> {
         guard !bytes.isEmpty, let lastByte = bytes.last else {
             return bytes
         }

+ 2 - 2
Sources/CryptoSwift/Padding.swift

@@ -7,6 +7,6 @@
 //
 
 public protocol Padding {
-    func add(data: Array<UInt8>, blockSize:Int) -> Array<UInt8>
-    func remove(data: Array<UInt8>, blockSize:Int?) -> Array<UInt8>
+    func add(to: Array<UInt8>, blockSize:Int) -> Array<UInt8>
+    func remove(from: Array<UInt8>, blockSize:Int?) -> Array<UInt8>
 }

+ 2 - 2
Sources/CryptoSwift/Poly1305.swift

@@ -84,9 +84,9 @@ final public class Poly1305 {
 
      - returns: Message Authentication Code
      */
-    public func authenticate(message:Array<UInt8>) -> Array<UInt8>? {
+    public func authenticate(bytes:Array<UInt8>) -> Array<UInt8>? {
         if let ctx = self.ctx {
-            update(context: ctx, message: message)
+            update(context: ctx, message: bytes)
             return finish(context: ctx)
         }
         return nil

+ 8 - 8
Sources/CryptoSwift/Rabbit.swift

@@ -134,14 +134,14 @@ final public class Rabbit: BlockCipher {
         
         // Iteration of the system
         var newX = Array<UInt32>(repeating: 0, count: 8)
-        newX[0] = g(j: 0) &+ rotateLeft(v: g(j: 7), 16) &+ rotateLeft(v: g(j: 6), 16)
-        newX[1] = g(j: 1) &+ rotateLeft(v: g(j: 0), 8)  &+ g(j: 7)
-        newX[2] = g(j: 2) &+ rotateLeft(v: g(j: 1), 16) &+ rotateLeft(v: g(j: 0), 16)
-        newX[3] = g(j: 3) &+ rotateLeft(v: g(j: 2), 8)  &+ g(j: 1)
-        newX[4] = g(j: 4) &+ rotateLeft(v: g(j: 3), 16) &+ rotateLeft(v: g(j: 2), 16)
-        newX[5] = g(j: 5) &+ rotateLeft(v: g(j: 4), 8)  &+ g(j: 3)
-        newX[6] = g(j: 6) &+ rotateLeft(v: g(j: 5), 16) &+ rotateLeft(v: g(j: 4), 16)
-        newX[7] = g(j: 7) &+ rotateLeft(v: g(j: 6), 8)  &+ g(j: 5)
+        newX[0] = g(j: 0) &+ rotateLeft(g(j: 7), by: 16) &+ rotateLeft(g(j: 6), by: 16)
+        newX[1] = g(j: 1) &+ rotateLeft(g(j: 0), by: 8)  &+ g(j: 7)
+        newX[2] = g(j: 2) &+ rotateLeft(g(j: 1), by: 16) &+ rotateLeft(g(j: 0), by: 16)
+        newX[3] = g(j: 3) &+ rotateLeft(g(j: 2), by: 8)  &+ g(j: 1)
+        newX[4] = g(j: 4) &+ rotateLeft(g(j: 3), by: 16) &+ rotateLeft(g(j: 2), by: 16)
+        newX[5] = g(j: 5) &+ rotateLeft(g(j: 4), by: 8)  &+ g(j: 3)
+        newX[6] = g(j: 6) &+ rotateLeft(g(j: 5), by: 16) &+ rotateLeft(g(j: 4), by: 16)
+        newX[7] = g(j: 7) &+ rotateLeft(g(j: 6), by: 8)  &+ g(j: 5)
         x = newX
     }
     

+ 3 - 3
Sources/CryptoSwift/SHA1.swift

@@ -40,7 +40,7 @@ final class SHA1 : HashProtocol {
                     M[x] = le.bigEndian
                     break
                 default:
-                    M[x] = rotateLeft(v: M[x-3] ^ M[x-8] ^ M[x-14] ^ M[x-16], 1) //FIXME: n:
+                    M[x] = rotateLeft(M[x-3] ^ M[x-8] ^ M[x-14] ^ M[x-16], by: 1)
                     break
                 }
             }
@@ -77,10 +77,10 @@ final class SHA1 : HashProtocol {
                     break
                 }
                 
-                let temp = (rotateLeft(v: A,5) &+ f &+ E &+ M[j] &+ k) & 0xffffffff
+                let temp = (rotateLeft(A, by: 5) &+ f &+ E &+ M[j] &+ k) & 0xffffffff
                 E = D
                 D = C
-                C = rotateLeft(v: B, 30)
+                C = rotateLeft(B, by: 30)
                 B = A
                 A = temp
             }

+ 10 - 10
Sources/CryptoSwift/SHA2.swift

@@ -136,12 +136,12 @@ final class SHA2 : HashProtocol {
                 case 0...15:
                     let start = chunk.startIndex + (x * sizeofValue(M[x]))
                     let end = start + sizeofValue(M[x])
-                    let le = toUInt32Array(slice: chunk[start..<end])[0]
+                    let le = sliceToUInt32Array(chunk[start..<end])[0]
                     M[x] = le.bigEndian
                     break
                 default:
-                    let s0 = rotateRight(x: M[x-15], n: 7) ^ rotateRight(x: M[x-15], n: 18) ^ (M[x-15] >> 3) //FIXME: n
-                    let s1 = rotateRight(x: M[x-2], n: 17) ^ rotateRight(x: M[x-2], n: 19) ^ (M[x-2] >> 10)
+                    let s0 = rotateRight(M[x-15], by: 7) ^ rotateRight(M[x-15], by: 18) ^ (M[x-15] >> 3)
+                    let s1 = rotateRight(M[x-2], by: 17) ^ rotateRight(M[x-2], by: 19) ^ (M[x-2] >> 10)
                     M[x] = M[x-16] &+ s0 &+ M[x-7] &+ s1
                     break
                 }
@@ -158,10 +158,10 @@ final class SHA2 : HashProtocol {
             
             // Main loop
             for j in 0..<variant.k.count {
-                let s0 = rotateRight(x: A,n: 2) ^ rotateRight(x: A,n: 13) ^ rotateRight(x: A,n: 22)
+                let s0 = rotateRight(A, by: 2) ^ rotateRight(A, by: 13) ^ rotateRight(A, by: 22)
                 let maj = (A & B) ^ (A & C) ^ (B & C)
                 let t2 = s0 &+ maj
-                let s1 = rotateRight(x: E,n: 6) ^ rotateRight(x: E,n: 11) ^ rotateRight(x: E,n: 25)
+                let s1 = rotateRight(E, by: 6) ^ rotateRight(E, by: 11) ^ rotateRight(E, by: 25)
                 let ch = (E & F) ^ ((~E) & G)
                 let t1 = H &+ s1 &+ ch &+ UInt32(variant.k[j]) &+ M[j]
                 
@@ -219,12 +219,12 @@ final class SHA2 : HashProtocol {
                 case 0...15:
                     let start = chunk.startIndex + (x * sizeofValue(M[x]))
                     let end = start + sizeofValue(M[x])
-                    let le = toUInt64Array(slice: chunk[start..<end])[0]
+                    let le = sliceToUInt64Array(chunk[start..<end])[0]
                     M[x] = le.bigEndian
                     break
                 default:
-                    let s0 = rotateRight(x: M[x-15], n: 1) ^ rotateRight(x: M[x-15], n: 8) ^ (x: M[x-15] >> 7)
-                    let s1 = rotateRight(x: M[x-2], n: 19) ^ rotateRight(x: M[x-2], n: 61) ^ (x: M[x-2] >> 6)
+                    let s0 = rotateRight(M[x-15], by: 1) ^ rotateRight(M[x-15], by: 8) ^ (M[x-15] >> 7)
+                    let s1 = rotateRight(M[x-2], by: 19) ^ rotateRight(M[x-2], by: 61) ^ (M[x-2] >> 6)
                     M[x] = M[x-16] &+ s0 &+ M[x-7] &+ s1
                     break
                 }
@@ -241,10 +241,10 @@ final class SHA2 : HashProtocol {
             
             // Main loop
             for j in 0..<variant.k.count {
-                let s0 = rotateRight(x: A,n: 28) ^ rotateRight(x: A,n: 34) ^ rotateRight(x: A,n: 39) //FIXME: n:
+                let s0 = rotateRight(A, by: 28) ^ rotateRight(A, by: 34) ^ rotateRight(A, by: 39) //FIXME: n:
                 let maj = (A & B) ^ (A & C) ^ (B & C)
                 let t2 = s0 &+ maj
-                let s1 = rotateRight(x: E,n: 14) ^ rotateRight(x: E,n: 18) ^ rotateRight(x: E,n: 41)
+                let s1 = rotateRight(E, by: 14) ^ rotateRight(E, by: 18) ^ rotateRight(E, by: 41)
                 let ch = (E & F) ^ ((~E) & G)
                 let t1 = H &+ s1 &+ ch &+ variant.k[j] &+ UInt64(M[j])
                 

+ 2 - 2
Sources/CryptoSwift/String+Extension.swift

@@ -50,7 +50,7 @@ extension String {
     }
     
     /// Returns hex string of bytes.
-    public func authenticate(authenticator: Authenticator) throws -> String {
-        return  try self.utf8.lazy.map({ $0 as UInt8 }).authenticate(authenticator: authenticator).toHexString()
+    public func authenticate(with authenticator: Authenticator) throws -> String {
+        return try self.utf8.lazy.map({ $0 as UInt8 }).authenticate(with: authenticator).toHexString()
     }
 }

+ 19 - 19
Sources/CryptoSwift/Utils.swift

@@ -6,43 +6,43 @@
 //  Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
 //
 
-func rotateLeft(v:UInt8, _ n:UInt8) -> UInt8 {
-    return ((v << n) & 0xFF) | (v >> (8 - n))
+func rotateLeft(_ value:UInt8, by:UInt8) -> UInt8 {
+    return ((value << by) & 0xFF) | (value >> (8 - by))
 }
 
-func rotateLeft(v:UInt16, _ n:UInt16) -> UInt16 {
-    return ((v << n) & 0xFFFF) | (v >> (16 - n))
+func rotateLeft(_ value:UInt16, by:UInt16) -> UInt16 {
+    return ((value << by) & 0xFFFF) | (value >> (16 - by))
 }
 
-func rotateLeft(v:UInt32, _ n:UInt32) -> UInt32 {
-    return ((v << n) & 0xFFFFFFFF) | (v >> (32 - n))
+func rotateLeft(_ value:UInt32, by:UInt32) -> UInt32 {
+    return ((value << by) & 0xFFFFFFFF) | (value >> (32 - by))
 }
 
-func rotateLeft(x:UInt64, _ n:UInt64) -> UInt64 {
-    return (x << n) | (x >> (64 - n))
+func rotateLeft(_ value:UInt64, by:UInt64) -> UInt64 {
+    return (value << by) | (value >> (64 - by))
 }
 
-func rotateRight(x:UInt16, n:UInt16) -> UInt16 {
-    return (x >> n) | (x << (16 - n))
+func rotateRight(_ value:UInt16, by:UInt16) -> UInt16 {
+    return (value >> by) | (value << (16 - by))
 }
 
-func rotateRight(x:UInt32, n:UInt32) -> UInt32 {
-    return (x >> n) | (x << (32 - n))
+func rotateRight(_ value:UInt32, by:UInt32) -> UInt32 {
+    return (value >> by) | (value << (32 - by))
 }
 
-func rotateRight(x:UInt64, n:UInt64) -> UInt64 {
-    return ((x >> n) | (x << (64 - n)))
+func rotateRight(_ value:UInt64, by:UInt64) -> UInt64 {
+    return ((value >> by) | (value << (64 - by)))
 }
 
-func reverseUInt8(uint8 : UInt8) -> UInt8 {
-    var v : UInt8 = uint8
+func reverse(uint8 : UInt8) -> UInt8 {
+    var v = uint8
     v = (v & 0xF0) >> 4 | (v & 0x0F) << 4
     v = (v & 0xCC) >> 2 | (v & 0x33) << 2
     v = (v & 0xAA) >> 1 | (v & 0x55) << 1
     return v
 }
 
-func reverseUInt32(uint32 : UInt32) -> UInt32 {
+func reverse(uint32 : UInt32) -> UInt32 {
     var v = uint32
     v = ((v >> 1) & 0x55555555) | ((v & 0x55555555) << 1)
     v = ((v >> 2) & 0x33333333) | ((v & 0x33333333) << 2)
@@ -52,7 +52,7 @@ func reverseUInt32(uint32 : UInt32) -> UInt32 {
     return v
 }
 
-func toUInt32Array(slice: ArraySlice<UInt8>) -> Array<UInt32> {
+func sliceToUInt32Array(_ slice: ArraySlice<UInt8>) -> Array<UInt32> {
     var result = Array<UInt32>()
     result.reserveCapacity(16)
     for idx in stride(from: slice.startIndex, to: slice.endIndex, by: sizeof(UInt32)) {
@@ -66,7 +66,7 @@ func toUInt32Array(slice: ArraySlice<UInt8>) -> Array<UInt32> {
     return result
 }
 
-func toUInt64Array(slice: ArraySlice<UInt8>) -> Array<UInt64> {
+func sliceToUInt64Array(_ slice: ArraySlice<UInt8>) -> Array<UInt64> {
     var result = Array<UInt64>()
     result.reserveCapacity(32)
     for idx in stride(from: slice.startIndex, to: slice.endIndex, by: sizeof(UInt64)) {