From 72734cd059ae17225f1f24aedf566098a2194f9c Mon Sep 17 00:00:00 2001 From: taylorswift Date: Thu, 30 May 2024 21:24:18 +0000 Subject: [PATCH] re-save all the files in VSCode to strip trailing whitespace --- Sources/Base16/Base16.LowercaseDigits.swift | 2 +- Sources/Base16/Base16.UppercaseDigits.swift | 2 +- Sources/Base16/Base16.Values.swift | 2 +- Sources/Base64/Base64.Digits.swift | 2 +- Sources/Base64/Base64.swift | 26 +-- Sources/Base64Tests/Base64Test.swift | 6 +- Sources/CRC/CRC32.swift | 50 ++--- .../MessageAuthenticationHash.swift | 4 +- .../MessageAuthenticationKey.swift | 10 +- Sources/SHA2/SHA256.swift | 180 +++++++++--------- 10 files changed, 142 insertions(+), 142 deletions(-) diff --git a/Sources/Base16/Base16.LowercaseDigits.swift b/Sources/Base16/Base16.LowercaseDigits.swift index 05db9f4..acb2917 100644 --- a/Sources/Base16/Base16.LowercaseDigits.swift +++ b/Sources/Base16/Base16.LowercaseDigits.swift @@ -9,7 +9,7 @@ extension Base16 } extension Base16.LowercaseDigits:BaseDigits { - @inlinable public static + @inlinable public static subscript(remainder:UInt8) -> UInt8 { (remainder < 10 ? 0x30 : 0x61 - 10) &+ remainder diff --git a/Sources/Base16/Base16.UppercaseDigits.swift b/Sources/Base16/Base16.UppercaseDigits.swift index 0b00979..5d7b3a3 100644 --- a/Sources/Base16/Base16.UppercaseDigits.swift +++ b/Sources/Base16/Base16.UppercaseDigits.swift @@ -9,7 +9,7 @@ extension Base16 } extension Base16.UppercaseDigits:BaseDigits { - @inlinable public static + @inlinable public static subscript(remainder:UInt8) -> UInt8 { (remainder < 10 ? 0x30 : 0x41 - 10) &+ remainder diff --git a/Sources/Base16/Base16.Values.swift b/Sources/Base16/Base16.Values.swift index 4ce7d70..52036a6 100644 --- a/Sources/Base16/Base16.Values.swift +++ b/Sources/Base16/Base16.Values.swift @@ -25,7 +25,7 @@ extension Base16.Values:Sequence, IteratorProtocol { while let digit:UInt8 = self.iterator.next() { - switch digit + switch digit { case 0x30 ... 0x39: return digit - 0x30 case 0x61 ... 0x66: return digit + 10 - 0x61 diff --git a/Sources/Base64/Base64.Digits.swift b/Sources/Base64/Base64.Digits.swift index 221a0ea..49e8d24 100644 --- a/Sources/Base64/Base64.Digits.swift +++ b/Sources/Base64/Base64.Digits.swift @@ -77,7 +77,7 @@ extension Base64 } extension Base64.Digits:BaseDigits { - @inlinable public static + @inlinable public static subscript(remainder:UInt8) -> UInt8 { Self.ascii[Int.init(remainder & 0b0011_1111)] diff --git a/Sources/Base64/Base64.swift b/Sources/Base64/Base64.swift index 8fad9e7..cb4c4f2 100644 --- a/Sources/Base64/Base64.swift +++ b/Sources/Base64/Base64.swift @@ -2,7 +2,7 @@ import BaseDigits /// A namespace for base-64 utilities. public -enum Base64 +enum Base64 { /// Decodes some ``String``-like type containing an ASCII-encoded base-64 string /// to some ``RangeReplaceableCollection`` type. Padding is not required. @@ -15,7 +15,7 @@ enum Base64 /// This function uses the size of the input string to provide a capacity hint /// for its output, and may over-allocate storage if the input contains many /// non-digit characters. - @inlinable public static + @inlinable public static func decode(_ ascii:ASCII, to _:Bytes.Type = Bytes.self) -> Bytes where Bytes:RangeReplaceableCollection, Bytes.Element == UInt8, ASCII:StringProtocol @@ -33,7 +33,7 @@ enum Base64 /// This function uses the size of the input string to provide a capacity hint /// for its output, and may over-allocate storage if the input contains many /// non-digit characters. - @inlinable public static + @inlinable public static func decode(_ ascii:ASCII, to _:Bytes.Type = Bytes.self) -> Bytes where Bytes:RangeReplaceableCollection, Bytes.Element == UInt8, ASCII:Sequence, ASCII.Element == UInt8 @@ -67,7 +67,7 @@ enum Base64 } /// Encodes a sequence of bytes to a base-64 string with padding if needed. - @inlinable public static + @inlinable public static func encode(_ bytes:Bytes) -> String where Bytes:Sequence, Bytes.Element == UInt8 { var encoded:String = "" @@ -77,28 +77,28 @@ enum Base64 { encoded.append( Digits[first >> 2]) - guard let second:UInt8 = bytes.next() - else + guard let second:UInt8 = bytes.next() + else { encoded.append(Digits[first << 4]) encoded.append("=") encoded.append("=") - continue + continue } - + encoded.append( Digits[first << 4 | second >> 4]) - guard let third:UInt8 = bytes.next() - else + guard let third:UInt8 = bytes.next() + else { encoded.append(Digits[second << 2]) encoded.append("=") - continue + continue } - + encoded.append( Digits[second << 2 | third >> 6]) encoded.append( Digits[third]) } - return encoded + return encoded } } diff --git a/Sources/Base64Tests/Base64Test.swift b/Sources/Base64Tests/Base64Test.swift index 728d99e..60dad7c 100644 --- a/Sources/Base64Tests/Base64Test.swift +++ b/Sources/Base64Tests/Base64Test.swift @@ -9,7 +9,7 @@ struct Base64Test init(name:String, degenerate:String? = nil, - canonical:String, + canonical:String, expected:[UInt8]) { self.name = name @@ -22,7 +22,7 @@ extension Base64Test { init(name:String, degenerate:String? = nil, - canonical:String, + canonical:String, expected:UTF8) where UTF8:Collection, UTF8.Element == UInt8 { self.init(name: name, @@ -32,7 +32,7 @@ extension Base64Test } init(name:String, degenerate:String? = nil, - canonical:String, + canonical:String, expected:String) { self.init(name: name, diff --git a/Sources/CRC/CRC32.swift b/Sources/CRC/CRC32.swift index 59a90f7..c0242f8 100644 --- a/Sources/CRC/CRC32.swift +++ b/Sources/CRC/CRC32.swift @@ -2,54 +2,54 @@ import Base16 #if swift(>=5.5) extension CRC32:Sendable {} -#endif +#endif @frozen public struct CRC32:Hashable { - public static - let table:[UInt32] = (0 ..< 256).map + public static + let table:[UInt32] = (0 ..< 256).map { - (i:UInt32) in + (i:UInt32) in (0 ..< 8).reduce(i){ (c, _) in (c & 1 * 0xed_b8_83_20) ^ c >> 1 } } - - public - var checksum:UInt32 - + + public + var checksum:UInt32 + @inlinable public init(checksum:UInt32 = 0) { - self.checksum = checksum + self.checksum = checksum } - @inlinable public + @inlinable public init(hashing message:Message) - where Message:Sequence, Message.Element == UInt8 + where Message:Sequence, Message.Element == UInt8 { self.init() self.update(with: message) } - - @inlinable public - func updated(with message:Message) -> Self - where Message:Sequence, Message.Element == UInt8 + + @inlinable public + func updated(with message:Message) -> Self + where Message:Sequence, Message.Element == UInt8 { - var checksum:Self = self + var checksum:Self = self checksum.update(with: message) return checksum } - @inlinable public mutating - func update(with message:Message) - where Message:Sequence, Message.Element == UInt8 + @inlinable public mutating + func update(with message:Message) + where Message:Sequence, Message.Element == UInt8 { - self.checksum = ~message.reduce(~self.checksum) + self.checksum = ~message.reduce(~self.checksum) { - (state:UInt32, byte:UInt8) in + (state:UInt32, byte:UInt8) in Self.table[Int.init(UInt8.init(truncatingIfNeeded: state) ^ byte)] ^ state >> 8 } } } -extension CRC32:ExpressibleByIntegerLiteral +extension CRC32:ExpressibleByIntegerLiteral { @inlinable public init(integerLiteral:UInt32) @@ -57,10 +57,10 @@ extension CRC32:ExpressibleByIntegerLiteral self.init(checksum: integerLiteral) } } -extension CRC32:CustomStringConvertible +extension CRC32:CustomStringConvertible { - @inlinable public - var description:String + @inlinable public + var description:String { Base16.encode(storing: self.checksum.bigEndian, with: Base16.LowercaseDigits.self) } diff --git a/Sources/MessageAuthentication/MessageAuthenticationHash.swift b/Sources/MessageAuthentication/MessageAuthenticationHash.swift index 0030af0..0aaa245 100644 --- a/Sources/MessageAuthentication/MessageAuthenticationHash.swift +++ b/Sources/MessageAuthentication/MessageAuthenticationHash.swift @@ -12,7 +12,7 @@ protocol MessageAuthenticationHash:RandomAccessCollection where Index == Int, El /// Computes an instance of this hash for the given message. init(hashing message:Message) - where Message:Collection, Message.Element == UInt8 + where Message:Collection, Message.Element == UInt8 } extension MessageAuthenticationHash { @@ -40,7 +40,7 @@ extension MessageAuthenticationHash @inlinable public init(authenticating message:Message, key:Key) where Message:Sequence, Message.Element == UInt8, - Key:Collection, Key.Element == UInt8 + Key:Collection, Key.Element == UInt8 { let key:MessageAuthenticationKey = .init(key) self = key.authenticate(message) diff --git a/Sources/MessageAuthentication/MessageAuthenticationKey.swift b/Sources/MessageAuthentication/MessageAuthenticationKey.swift index 265e5f0..cb4dd0b 100644 --- a/Sources/MessageAuthentication/MessageAuthenticationKey.swift +++ b/Sources/MessageAuthentication/MessageAuthenticationKey.swift @@ -14,7 +14,7 @@ struct MessageAuthenticationKey where Hash:MessageAuthenticationHash /// Creates a message authentication key from the given base key. @inlinable public - init(_ key:Key) where Key:Collection, Key.Element == UInt8 + init(_ key:Key) where Key:Collection, Key.Element == UInt8 { let normalized:[UInt8] let count:Int = key.count @@ -27,11 +27,11 @@ struct MessageAuthenticationKey where Hash:MessageAuthenticationHash { normalized = [UInt8].init(key) + repeatElement(0, count: Hash.stride - count) } - else + else { normalized = [UInt8].init(key) } - + self.inner = normalized.map { $0 ^ 0x36 } self.outer = normalized.map { $0 ^ 0x5c } } @@ -63,7 +63,7 @@ extension MessageAuthenticationKey var hash:Hash = self.authenticate(salt) var block:[UInt8] = .init(hash) - + for _ in 1 ..< iterations { hash = self.authenticate(hash) @@ -72,7 +72,7 @@ extension MessageAuthenticationKey block[index] ^= byte } } - + output.append(contentsOf: block) } return output diff --git a/Sources/SHA2/SHA256.swift b/Sources/SHA2/SHA256.swift index 8f3f548..d3d443a 100644 --- a/Sources/SHA2/SHA256.swift +++ b/Sources/SHA2/SHA256.swift @@ -3,40 +3,40 @@ import MessageAuthentication #if swift(>=5.5) extension SHA256:Sendable {} -#endif +#endif @frozen public struct SHA256 { - public + public typealias Words = (UInt32, UInt32, UInt32, UInt32, UInt32, UInt32, UInt32, UInt32) - - public static - let table:[UInt32] = + + public static + let table:[UInt32] = [ - 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, + 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, - 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, + 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, - 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, + 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, - 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, + 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, - 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, + 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, - 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, + 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, - 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, + 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, - 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, + 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2, ] - + public var words:Words - - @inlinable public - init(words:Words = + + @inlinable public + init(words:Words = ( 0x6a09e667, 0xbb67ae85, @@ -46,101 +46,101 @@ struct SHA256 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 - )) + )) { - self.words = words + self.words = words } - - @inlinable public + + @inlinable public init(hashing message:Message) - where Message:Collection, Message.Element == UInt8 + where Message:Collection, Message.Element == UInt8 { self.init() - + var blocks:Int = 0 - var start:Message.Index = message.startIndex - while let end:Message.Index = + var start:Message.Index = message.startIndex + while let end:Message.Index = message.index(start, offsetBy: 64, limitedBy: message.endIndex) { self.update(with: message[start ..< end]) blocks += 1 - start = end + start = end } - + var epilogue:[UInt8] = [] epilogue.reserveCapacity(128) epilogue.append(contentsOf: message[start...]) - - let remaining:Int = epilogue.count - // 1 byte separator + 8 bytes UInt64 + however many more it takes to + + let remaining:Int = epilogue.count + // 1 byte separator + 8 bytes UInt64 + however many more it takes to // get to a multiple of 64 let padding:Int = (119 - remaining) & 63 let count:Int = blocks << 6 | remaining - + epilogue.append(0x80) epilogue.append(contentsOf: repeatElement(0x00, count: padding)) withUnsafeBytes(of: UInt64.init(count << 3).bigEndian) { epilogue.append(contentsOf: $0) } - if epilogue.count == 128 + if epilogue.count == 128 { self.update(with: epilogue[..<64]) self.update(with: epilogue[64...]) } - else + else { self.update(with: epilogue) } - } - - @inlinable public mutating - func update(with chunk:Chunk) + } + + @inlinable public mutating + func update(with chunk:Chunk) where Chunk:Collection, Chunk.Element == UInt8 { assert(chunk.count == 64) - + let schedule:[UInt32] = .init(unsafeUninitializedCapacity: 64) { - (buffer:inout UnsafeMutableBufferPointer, count:inout Int) in - - count = 64 - - var base:Chunk.Index = chunk.startIndex - for i:Int in 0 ..< 16 + (buffer:inout UnsafeMutableBufferPointer, count:inout Int) in + + count = 64 + + var base:Chunk.Index = chunk.startIndex + for i:Int in 0 ..< 16 { - // big-endian + // big-endian let c:Chunk.Index = chunk.index(after: base) let b:Chunk.Index = chunk.index(after: c) let a:Chunk.Index = chunk.index(after: b) - buffer[i] = + buffer[i] = UInt32.init(chunk[base]) << 24 | UInt32.init(chunk[c]) << 16 | UInt32.init(chunk[b]) << 8 | UInt32.init(chunk[a]) base = chunk.index(after: a) } - for i:Int in 16 ..< count + for i:Int in 16 ..< count { let s:(UInt32, UInt32) - s.0 = Self.rotate(buffer[i - 15], right: 7) ^ - Self.rotate(buffer[i - 15], right: 18) ^ + s.0 = Self.rotate(buffer[i - 15], right: 7) ^ + Self.rotate(buffer[i - 15], right: 18) ^ (buffer[i - 15] >> 3 as UInt32) s.1 = Self.rotate(buffer[i - 2], right: 17) ^ - Self.rotate(buffer[i - 2], right: 19) ^ + Self.rotate(buffer[i - 2], right: 19) ^ (buffer[i - 2] >> 10 as UInt32) let t:UInt32 = s.0 &+ s.1 buffer[i] = buffer[i - 16] &+ buffer[i - 7] &+ t } } - + var (a, b, c, d, e, f, g, h):Words = self.words - - for i:Int in 0 ..< 64 + + for i:Int in 0 ..< 64 { - let s:(UInt32, UInt32) - s.1 = Self.rotate(e, right: 6) ^ - Self.rotate(e, right: 11) ^ + let s:(UInt32, UInt32) + s.1 = Self.rotate(e, right: 6) ^ + Self.rotate(e, right: 11) ^ Self.rotate(e, right: 25) s.0 = Self.rotate(a, right: 2) ^ Self.rotate(a, right: 13) ^ @@ -150,7 +150,7 @@ struct SHA256 temp.0 = h &+ s.1 &+ ch &+ Self.table[i] &+ schedule[i] let maj:UInt32 = (a & b) ^ (a & c) ^ (b & c) temp.1 = maj &+ s.0 - + h = g g = f f = e @@ -160,7 +160,7 @@ struct SHA256 b = a a = temp.0 &+ temp.1 } - + self.words.0 &+= a self.words.1 &+= b self.words.2 &+= c @@ -170,9 +170,9 @@ struct SHA256 self.words.6 &+= g self.words.7 &+= h } - - @inlinable public static - func rotate(_ value:UInt32, right shift:Int) -> UInt32 + + @inlinable public static + func rotate(_ value:UInt32, right shift:Int) -> UInt32 { (value >> shift) | (value << (UInt32.bitWidth - shift)) } @@ -180,8 +180,8 @@ struct SHA256 extension SHA256:Equatable { - @inlinable public static - func == (lhs:Self, rhs:Self) -> Bool + @inlinable public static + func == (lhs:Self, rhs:Self) -> Bool { lhs.words.0 == rhs.words.0 && lhs.words.1 == rhs.words.1 && @@ -195,7 +195,7 @@ extension SHA256:Equatable } extension SHA256:Hashable { - @inlinable public + @inlinable public func hash(into hasher:inout Hasher) { self.words.0.hash(into: &hasher) @@ -216,47 +216,47 @@ extension SHA256:MessageAuthenticationHash let count:Int = 32 @inlinable public - var startIndex:Int + var startIndex:Int { 0 } - @inlinable public - subscript(index:Int) -> UInt8 + @inlinable public + subscript(index:Int) -> UInt8 { withUnsafePointer(to: self.words) { $0.withMemoryRebound(to: UInt32.self, capacity: 8) { - // big-endian + // big-endian UInt8.init(($0[index >> 2] << ((index & 3) << 3)) >> 24) } } } } -extension SHA256:ExpressibleByStringLiteral +extension SHA256:ExpressibleByStringLiteral { - @inlinable public + @inlinable public init?(parsing ascii:ASCII) where ASCII:Sequence, ASCII.Element == UInt8 { #if swift(>=5.6) guard let words:Words = Base16.decode(ascii, loading: Words.self) - else + else { return nil } - #else + #else var words:Words = (0, 0, 0, 0, 0, 0, 0, 0) - guard let _:Void = withUnsafeMutableBytes(of: &words, - { - return Base16.decode(ascii, into: $0) + guard let _:Void = withUnsafeMutableBytes(of: &words, + { + return Base16.decode(ascii, into: $0) }) - else + else { return nil } #endif - self.init(words: + self.init(words: ( .init(bigEndian: words.0), .init(bigEndian: words.1), @@ -268,34 +268,34 @@ extension SHA256:ExpressibleByStringLiteral .init(bigEndian: words.7) )) } - - @inlinable public + + @inlinable public init(stringLiteral:String) { if let hash:Self = Self.init(parsing: stringLiteral.utf8) { self = hash } - else + else { fatalError("invalid hex literal '\(stringLiteral)'") } } } -extension SHA256:CustomStringConvertible +extension SHA256:CustomStringConvertible { - @inlinable public - var description:String + @inlinable public + var description:String { Base16.encode(storing: ( - self.words.0.bigEndian, - self.words.1.bigEndian, - self.words.2.bigEndian, - self.words.3.bigEndian, - self.words.4.bigEndian, - self.words.5.bigEndian, - self.words.6.bigEndian, + self.words.0.bigEndian, + self.words.1.bigEndian, + self.words.2.bigEndian, + self.words.3.bigEndian, + self.words.4.bigEndian, + self.words.5.bigEndian, + self.words.6.bigEndian, self.words.7.bigEndian ), with: Base16.LowercaseDigits.self)