Skip to content

Instantly share code, notes, and snippets.

@hfossli
Last active June 28, 2022 09:52
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hfossli/bb7b1f2fe6e89c5dc6ae40dfd2be1122 to your computer and use it in GitHub Desktop.
Save hfossli/bb7b1f2fe6e89c5dc6ae40dfd2be1122 to your computer and use it in GitHub Desktop.
SHA256 in Swift
// This is a heavily altered version of SHA2.swift found in CryptoSwift.
// I tried to remove everything that is not about SHA256.
//
// --========================================================================--
//
// SHA2.swift
// CryptoSwift
//
// Created by Marcin Krzyzanowski on 24/08/14.
// Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
//
// Copyright (C) 2014 Marcin Krzyżanowski <marcin.krzyzanowski@gmail.com>
// This software is provided 'as-is', without any express or implied warranty.
//
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,including commercial applications,
// and to alter it and redistribute it freely, subject to the following restrictions:
//
// - The origin of this software must not be misrepresented; you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment in the product documentation is required.
// - Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
// - This notice may not be removed or altered from any source or binary distribution.
import Foundation
public extension Data {
func sha256() -> Data {
let bytes: Array<UInt8> = Array(self)
let result = SHA256(bytes).calculate32()
return Data(bytes: result)
}
}
final public class SHA256 {
let message: Array<UInt8>
init(_ message: Array<UInt8>) {
self.message = message
}
func calculate32() -> Array<UInt8> {
var tmpMessage = bitPadding(to: self.message, blockSize: 64, allowance: 64 / 8)
// hash values
var hh = Array<UInt32>()
h.forEach {(h) -> () in
hh.append(UInt32(h))
}
// append message length, in a 64-bit big-endian integer. So now the message length is a multiple of 512 bits.
tmpMessage += arrayOfBytes(value: message.count * 8, length: 64 / 8)
// Process the message in successive 512-bit chunks:
let chunkSizeBytes = 512 / 8 // 64
for chunk in BytesSequence(chunkSize: chunkSizeBytes, data: tmpMessage) {
// break chunk into sixteen 32-bit words M[j], 0 ≤ j ≤ 15, big-endian
// Extend the sixteen 32-bit words into sixty-four 32-bit words:
var M = Array<UInt32>(repeating: 0, count: k.count)
for x in 0..<M.count {
switch x {
case 0...15:
let start = chunk.startIndex + (x * MemoryLayout<UInt32>.size)
let end = start + MemoryLayout<UInt32>.size
let le = chunk[start..<end].toUInt32Array()[0]
M[x] = le.bigEndian
break
default:
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
}
}
var A = hh[0]
var B = hh[1]
var C = hh[2]
var D = hh[3]
var E = hh[4]
var F = hh[5]
var G = hh[6]
var H = hh[7]
// Main loop
for j in 0..<k.count {
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(E, by: 6) ^ rotateRight(E, by: 11) ^ rotateRight(E, by: 25)
let ch = (E & F) ^ ((~E) & G)
let t1 = H &+ s1 &+ ch &+ UInt32(k[j]) &+ M[j]
H = G
G = F
F = E
E = D &+ t1
D = C
C = B
B = A
A = t1 &+ t2
}
hh[0] = (hh[0] &+ A)
hh[1] = (hh[1] &+ B)
hh[2] = (hh[2] &+ C)
hh[3] = (hh[3] &+ D)
hh[4] = (hh[4] &+ E)
hh[5] = (hh[5] &+ F)
hh[6] = (hh[6] &+ G)
hh[7] = (hh[7] &+ H)
}
// Produce the final hash value (big-endian) as a 160 bit number:
var result = Array<UInt8>()
result.reserveCapacity(hh.count / 4)
ArraySlice(hh).forEach {
let item = $0.bigEndian
let toAppend: [UInt8] = [UInt8(item & 0xff), UInt8((item >> 8) & 0xff), UInt8((item >> 16) & 0xff), UInt8((item >> 24) & 0xff)]
result += toAppend
}
return result
}
private lazy var h: Array<UInt64> = {
return [0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19]
}()
private lazy var k: Array<UInt64> = {
return [0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2]
}()
private func rotateRight(_ value: UInt32, by: UInt32) -> UInt32 {
return (value >> by) | (value << (32 - by))
}
private func arrayOfBytes<T>(value: T, length: Int? = nil) -> Array<UInt8> {
let totalBytes = length ?? MemoryLayout<T>.size
let valuePointer = UnsafeMutablePointer<T>.allocate(capacity: 1)
valuePointer.pointee = value
let bytesPointer = UnsafeMutablePointer<UInt8>(OpaquePointer(valuePointer))
var bytes = Array<UInt8>(repeating: 0, count: totalBytes)
for j in 0..<min(MemoryLayout<T>.size, totalBytes) {
bytes[totalBytes - 1 - j] = (bytesPointer + j).pointee
}
valuePointer.deinitialize()
valuePointer.deallocate(capacity: 1)
return bytes
}
}
internal extension Collection where Self.Iterator.Element == UInt8, Self.Index == Int {
func toUInt32Array() -> Array<UInt32> {
var result = Array<UInt32>()
result.reserveCapacity(16)
for idx in stride(from: self.startIndex, to: self.endIndex, by: MemoryLayout<UInt32>.size) {
var val: UInt32 = 0
val |= self.count > 3 ? UInt32(self[idx.advanced(by: 3)]) << 24 : 0
val |= self.count > 2 ? UInt32(self[idx.advanced(by: 2)]) << 16 : 0
val |= self.count > 1 ? UInt32(self[idx.advanced(by: 1)]) << 8 : 0
val |= !self.isEmpty ? UInt32(self[idx]) : 0
result.append(val)
}
return result
}
}
internal func bitPadding(to data: Array<UInt8>, blockSize: Int, allowance: Int = 0) -> Array<UInt8> {
var tmp = data
// Step 1. Append Padding Bits
tmp.append(0x80) // append one bit (UInt8 with one bit) to message
// append "0" bit until message length in bits ≡ 448 (mod 512)
var msgLength = tmp.count
var counter = 0
while msgLength % blockSize != (blockSize - allowance) {
counter += 1
msgLength += 1
}
tmp += Array<UInt8>(repeating: 0, count: counter)
return tmp
}
internal struct BytesSequence<D: RandomAccessCollection>: Sequence where D.Iterator.Element == UInt8,
D.IndexDistance == Int,
D.SubSequence.IndexDistance == Int,
D.Index == Int {
let chunkSize: D.IndexDistance
let data: D
func makeIterator() -> AnyIterator<D.SubSequence> {
var offset = data.startIndex
return AnyIterator {
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
}
return nil
}
}
}
import XCTest
class SHA256Test: XCTestCase {
func testEmpty() {
let msg = "".data(using: .utf8)!
let result = msg.sha256().toHexString()
let expected = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" // echo -n "" | openssl dgst -sha256
XCTAssertEqual(result, expected, "Invalid conversion from msg to sha256")
}
func testAgainstKnownValue() {
let msg = "foobar".data(using: .utf8)!
let result = msg.sha256().toHexString()
let expected = "c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2" // echo -n "foobar" | openssl dgst -sha256
XCTAssertEqual(result, expected, "Invalid conversion from msg to sha256")
}
func testAgainstKnownValue2() {
let msg = "æøå".data(using: .utf8)!
let result = msg.sha256().toHexString()
let expected = "6c228cdba89548a1af198f33819536422fb01b66e51f761cf2ec38d1fb4178a6" // echo -n "æøå" | openssl dgst -sha256
XCTAssertEqual(result, expected, "Invalid conversion from msg to sha256")
}
func testAgainstKnownValue3() {
let msg = "KfZ=Day*q4MsZ=_xRy4G_Uefk?^Ytr&2xL*RYY%VLyB_&c7R_dr&J+8A79suf=^".data(using: .utf8)!
let result = msg.sha256().toHexString()
let expected = "b754632a872b3f5ddb0e1e24b531e35eb334ee3c2957618ac4a2ac4047ed6127" // echo -n "KfZ=Day*q4MsZ=_xRy4G_Uefk?^Ytr&2xL*RYY%VLyB_&c7R_dr&J+8A79suf=^" | openssl dgst -sha256
XCTAssertEqual(result, expected, "Invalid conversion from msg to sha256")
}
func testAgainstKnownValue4() {
let msg = "Lorem ipsum dolor sit amet, suas consequuntur mei ad, duo eu noluisse adolescens temporibus. Mutat fuisset constituam te vis. Animal meliore cu has, ius ad recusabo complectitur. Eam at persius inermis sensibus. Mea at velit nobis dolor, vitae omnium eos an, ei dolorum pertinacia nec.".data(using: .utf8)!
let result = msg.sha256().toHexString()
let expected = "31902eb17aa07165b645553c14b985c1908c7d8f4f5178de61a3232f09940df7" // echo -n "Lorem ipsum dolor sit amet, suas consequuntur mei ad, duo eu noluisse adolescens temporibus. Mutat fuisset constituam te vis. Animal meliore cu has, ius ad recusabo complectitur. Eam at persius inermis sensibus. Mea at velit nobis dolor, vitae omnium eos an, ei dolorum pertinacia nec." | openssl dgst -sha256
XCTAssertEqual(result, expected, "Invalid conversion from msg to sha256")
}
func testAgainstKnownValue5() {
let msg = "0".data(using: .utf8)!
let result = msg.sha256().toHexString()
let expected = "5feceb66ffc86f38d952786c6d696c79c2dbc239dd4e91b46729d73a27fb57e9" // echo -n "0" | openssl dgst -sha256
XCTAssertEqual(result, expected, "Invalid conversion from msg to sha256")
}
static var allTests = [
("testEmpty", testEmpty),
("testAgainstKnownValue", testAgainstKnownValue),
("testAgainstKnownValue2", testAgainstKnownValue2),
("testAgainstKnownValue3", testAgainstKnownValue3),
("testAgainstKnownValue4", testAgainstKnownValue4),
("testAgainstKnownValue5", testAgainstKnownValue5),
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment