2
0
Эх сурвалжийг харах

Use more overflow operators to avoid runtime checks

Marcin Krzyżanowski 9 жил өмнө
parent
commit
594f8f8694

+ 2 - 2
Sources/CryptoSwift/BytesSequence.swift

@@ -33,8 +33,8 @@ struct BytesSequence: Sequence {
     func makeIterator() -> AnyIterator<ArraySlice<UInt8>> {
         var offset = data.startIndex
         return AnyIterator {
-            let end = Swift.min(self.chunkSize, self.data.count - offset)
-            let result = self.data[offset..<offset + end]
+            let end = Swift.min(self.chunkSize, self.data.count &- offset)
+            let result = self.data[offset..<offset &+ end]
             offset = offset.advanced(by: result.count)
             if !result.isEmpty {
                 return result

+ 1 - 1
Sources/CryptoSwift/SHA1.swift

@@ -95,7 +95,7 @@ extension SHA1: Updatable {
     public func update<T: Sequence>(withBytes bytes: T, isLast: Bool = false) throws -> Array<UInt8> where T.Iterator.Element == UInt8 {
         let prevAccumulatedLength = self.accumulated.count
         self.accumulated += bytes
-        self.accumulatedLength += self.accumulated.count - prevAccumulatedLength //avoid Array(bytes).count
+        self.accumulatedLength = self.accumulatedLength &+ self.accumulated.count &- prevAccumulatedLength //avoid Array(bytes).count
 
         if isLast {
             // Step 1. Append padding

+ 1 - 1
Sources/CryptoSwift/SHA2.swift

@@ -261,7 +261,7 @@ extension SHA2: Updatable {
     public func update<T: Sequence>(withBytes bytes: T, isLast: Bool = false) throws -> Array<UInt8> where T.Iterator.Element == UInt8 {
         let prevAccumulatedLength = self.accumulated.count
         self.accumulated += bytes
-        self.accumulatedLength += self.accumulated.count - prevAccumulatedLength //avoid Array(bytes).count
+        self.accumulatedLength = self.accumulatedLength &+ self.accumulated.count &- prevAccumulatedLength //avoid Array(bytes).count
 
         if isLast {
             // Step 1. Append padding

+ 1 - 1
Sources/CryptoSwift/SHA3.swift

@@ -241,7 +241,7 @@ extension SHA3: Updatable {
     public func update<T: Sequence>(withBytes bytes: T, isLast: Bool = false) throws -> Array<UInt8> where T.Iterator.Element == UInt8 {
         let prevAccumulatedLength = self.accumulated.count
         self.accumulated += bytes
-        self.accumulatedLength += self.accumulated.count - prevAccumulatedLength //avoid Array(bytes).count
+        self.accumulatedLength = self.accumulatedLength &+ self.accumulated.count &- prevAccumulatedLength //avoid Array(bytes).count
 
         if isLast {
             // Add padding