|
| 1 | +// |
| 2 | +// This source file is part of the Swift.org open source project |
| 3 | +// |
| 4 | +// Copyright (c) 2014–2025 Apple Inc. and the Swift project authors |
| 5 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 6 | +// |
| 7 | +// See https://swift.org/LICENSE.txt for license information |
| 8 | +// See https://swift.org/CONTRIBUTORS.txt for Swift project authors |
| 9 | +// |
| 10 | + |
| 11 | +/// The contents of this file were copied more-or-less verbatim from |
| 12 | +/// [swift-tools-support-core](https://github.com/swiftlang/swift-tools-support-core/blob/add9e1518ac37a8e52b7612d3eb2f009ae8f6ce8/Sources/TSCBasic/HashAlgorithms.swift). |
| 13 | + |
| 14 | +/// SHA-256 implementation from Secure Hash Algorithm 2 (SHA-2) set of |
| 15 | +/// cryptographic hash functions (FIPS PUB 180-2). |
| 16 | +enum SHA256 { |
| 17 | + /// The length of the output digest (in bits). |
| 18 | + private static let _digestLength = 256 |
| 19 | + |
| 20 | + /// The size of each blocks (in bits). |
| 21 | + private static let _blockBitSize = 512 |
| 22 | + |
| 23 | + /// The initial hash value. |
| 24 | + private static let _initialHashValue: [UInt32] = [ |
| 25 | + 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 |
| 26 | + ] |
| 27 | + |
| 28 | + /// The constants in the algorithm (K). |
| 29 | + private static let _konstants: [UInt32] = [ |
| 30 | + 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, |
| 31 | + 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, |
| 32 | + 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, |
| 33 | + 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, |
| 34 | + 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, |
| 35 | + 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, |
| 36 | + 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, |
| 37 | + 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 |
| 38 | + ] |
| 39 | + |
| 40 | + public static func hash(_ bytes: some Sequence<UInt8>) -> [UInt8] { |
| 41 | + var input = Array(bytes) |
| 42 | + |
| 43 | + // Pad the input. |
| 44 | + _pad(&input) |
| 45 | + |
| 46 | + // Break the input into N 512-bit blocks. |
| 47 | + let messageBlocks = input.blocks(size: _blockBitSize / 8) |
| 48 | + |
| 49 | + /// The hash that is being computed. |
| 50 | + var hash = _initialHashValue |
| 51 | + |
| 52 | + // Process each block. |
| 53 | + for block in messageBlocks { |
| 54 | + _process(block, hash: &hash) |
| 55 | + } |
| 56 | + |
| 57 | + // Finally, compute the result. |
| 58 | + var result = [UInt8](repeating: 0, count: _digestLength / 8) |
| 59 | + for (idx, element) in hash.enumerated() { |
| 60 | + let pos = idx * 4 |
| 61 | + result[pos + 0] = UInt8((element >> 24) & 0xff) |
| 62 | + result[pos + 1] = UInt8((element >> 16) & 0xff) |
| 63 | + result[pos + 2] = UInt8((element >> 8) & 0xff) |
| 64 | + result[pos + 3] = UInt8(element & 0xff) |
| 65 | + } |
| 66 | + |
| 67 | + return result |
| 68 | + } |
| 69 | + |
| 70 | + /// Process and compute hash from a block. |
| 71 | + private static func _process(_ block: ArraySlice<UInt8>, hash: inout [UInt32]) { |
| 72 | + |
| 73 | + // Compute message schedule. |
| 74 | + var W = [UInt32](repeating: 0, count: _konstants.count) |
| 75 | + for t in 0..<W.count { |
| 76 | + switch t { |
| 77 | + case 0...15: |
| 78 | + let index = block.startIndex.advanced(by: t * 4) |
| 79 | + // Put 4 bytes in each message. |
| 80 | + W[t] = UInt32(block[index + 0]) << 24 |
| 81 | + W[t] |= UInt32(block[index + 1]) << 16 |
| 82 | + W[t] |= UInt32(block[index + 2]) << 8 |
| 83 | + W[t] |= UInt32(block[index + 3]) |
| 84 | + default: |
| 85 | + let σ1 = W[t-2].rotateRight(by: 17) ^ W[t-2].rotateRight(by: 19) ^ (W[t-2] >> 10) |
| 86 | + let σ0 = W[t-15].rotateRight(by: 7) ^ W[t-15].rotateRight(by: 18) ^ (W[t-15] >> 3) |
| 87 | + W[t] = σ1 &+ W[t-7] &+ σ0 &+ W[t-16] |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + var a = hash[0] |
| 92 | + var b = hash[1] |
| 93 | + var c = hash[2] |
| 94 | + var d = hash[3] |
| 95 | + var e = hash[4] |
| 96 | + var f = hash[5] |
| 97 | + var g = hash[6] |
| 98 | + var h = hash[7] |
| 99 | + |
| 100 | + // Run the main algorithm. |
| 101 | + for t in 0..<_konstants.count { |
| 102 | + let Σ1 = e.rotateRight(by: 6) ^ e.rotateRight(by: 11) ^ e.rotateRight(by: 25) |
| 103 | + let ch = (e & f) ^ (~e & g) |
| 104 | + let t1 = h &+ Σ1 &+ ch &+ _konstants[t] &+ W[t] |
| 105 | + |
| 106 | + let Σ0 = a.rotateRight(by: 2) ^ a.rotateRight(by: 13) ^ a.rotateRight(by: 22) |
| 107 | + let maj = (a & b) ^ (a & c) ^ (b & c) |
| 108 | + let t2 = Σ0 &+ maj |
| 109 | + |
| 110 | + h = g |
| 111 | + g = f |
| 112 | + f = e |
| 113 | + e = d &+ t1 |
| 114 | + d = c |
| 115 | + c = b |
| 116 | + b = a |
| 117 | + a = t1 &+ t2 |
| 118 | + } |
| 119 | + |
| 120 | + hash[0] = a &+ hash[0] |
| 121 | + hash[1] = b &+ hash[1] |
| 122 | + hash[2] = c &+ hash[2] |
| 123 | + hash[3] = d &+ hash[3] |
| 124 | + hash[4] = e &+ hash[4] |
| 125 | + hash[5] = f &+ hash[5] |
| 126 | + hash[6] = g &+ hash[6] |
| 127 | + hash[7] = h &+ hash[7] |
| 128 | + } |
| 129 | + |
| 130 | + /// Pad the given byte array to be a multiple of 512 bits. |
| 131 | + private static func _pad(_ input: inout [UInt8]) { |
| 132 | + // Find the bit count of input. |
| 133 | + let inputBitLength = input.count * 8 |
| 134 | + |
| 135 | + // Append the bit 1 at end of input. |
| 136 | + input.append(0x80) |
| 137 | + |
| 138 | + // Find the number of bits we need to append. |
| 139 | + // |
| 140 | + // inputBitLength + 1 + bitsToAppend ≡ 448 mod 512 |
| 141 | + let mod = inputBitLength % 512 |
| 142 | + let bitsToAppend = mod < 448 ? 448 - 1 - mod : 512 + 448 - mod - 1 |
| 143 | + |
| 144 | + // We already appended first 7 bits with 0x80 above. |
| 145 | + input += [UInt8](repeating: 0, count: (bitsToAppend - 7) / 8) |
| 146 | + |
| 147 | + // We need to append 64 bits of input length. |
| 148 | + for byte in UInt64(inputBitLength).toByteArray().lazy.reversed() { |
| 149 | + input.append(byte) |
| 150 | + } |
| 151 | + assert((input.count * 8) % 512 == 0, "Expected padded length to be 512.") |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +// MARK:- Helpers |
| 156 | + |
| 157 | +extension UInt64 { |
| 158 | + /// Converts the 64 bit integer into an array of single byte integers. |
| 159 | + fileprivate func toByteArray() -> [UInt8] { |
| 160 | + var value = self.littleEndian |
| 161 | + return withUnsafeBytes(of: &value, Array.init) |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +extension UInt32 { |
| 166 | + /// Rotates self by given amount. |
| 167 | + fileprivate func rotateRight(by amount: UInt32) -> UInt32 { |
| 168 | + return (self >> amount) | (self << (32 - amount)) |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +extension Array { |
| 173 | + /// Breaks the array into the given size. |
| 174 | + fileprivate func blocks(size: Int) -> AnyIterator<ArraySlice<Element>> { |
| 175 | + var currentIndex = startIndex |
| 176 | + return AnyIterator { |
| 177 | + if let nextIndex = self.index(currentIndex, offsetBy: size, limitedBy: self.endIndex) { |
| 178 | + defer { currentIndex = nextIndex } |
| 179 | + return self[currentIndex..<nextIndex] |
| 180 | + } |
| 181 | + return nil |
| 182 | + } |
| 183 | + } |
| 184 | +} |
0 commit comments