forked from imkcat/CatCrypto
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SHA.swift
161 lines (128 loc) · 4.89 KB
/
SHA.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//
// SHA.swift
// CatCrypto
//
// Created by Kcat on 2017/12/28.
// Copyright © 2017年 imkcat. All rights reserved.
//
// https://github.com/ImKcat/CatCrypto
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
import Foundation
import CommonCrypto
import SHA3
/// `CatSHA1Crypto` is the crypto for
/// [SHA-1](https://csrc.nist.gov/csrc/media/publications/fips/180/4/final/documents/fips180-4-draft-aug2014.pdf) function.
public class CatSHA1Crypto: CatCCHashingCrypto {
public override init() {
super.init()
algorithm = .sha1
}
}
/// Desired bit-length of the hash function output.
public enum CatSHA2HashLength {
/// 224 bits.
case bit224
/// 256 bits.
case bit256
/// 384 bits.
case bit384
/// 512 bits.
case bit512
}
/// Context for SHA-2 crypto.
public class CatSHA2Context {
/// Desired bit-length of the hash function output.
public var hashLength: CatSHA2HashLength = .bit512
/// Initialize the context.
public init() {}
}
/// `CatSHA2Crypto` is the crypto for
/// [SHA-2](https://csrc.nist.gov/csrc/media/publications/fips/180/4/final/documents/fips180-4-draft-aug2014.pdf) function.
public class CatSHA2Crypto: CatCCHashingCrypto, Contextual {
public typealias Context = CatSHA2Context
public var context: CatSHA2Context
public required init(context: Context = CatSHA2Context()) {
self.context = context
super.init()
}
// MARK: - Hashing
public override func hash(password: String) -> CatCryptoResult {
switch context.hashLength {
case .bit224: algorithm = .sha224
case .bit256: algorithm = .sha256
case .bit384: algorithm = .sha384
case .bit512: algorithm = .sha512
}
return super.hash(password: password)
}
}
/// Desired bit-length of the hash function output.
public enum CatSHA3HashLength: Int {
/// 224 bits.
case bit224 = 28
/// 256 bits.
case bit256 = 32
/// 384 bits.
case bit384 = 48
/// 512 bits.
case bit512 = 64
}
/// Context for SHA-3 crypto.
public struct CatSHA3Context {
/// Desired bit-length of the hash function output.
public var hashLength: CatSHA3HashLength = .bit512
/// Initialize the context.
public init() {}
}
/// `CatSHA2Crypto` is the crypto for
/// [SHA-3](http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.202.pdf) function.
public class CatSHA3Crypto: Contextual, Hashing {
// MARK: - Contextual
public typealias Context = CatSHA3Context
public var context: CatSHA3Context
public required init(context: CatSHA3Context = CatSHA3Context()) {
self.context = context
}
// MARK: - Core
/// Hash with SHA-3 function.
///
/// - Parameter password: Password string.
/// - Returns: Return a tuple that include error code and raw output.
func sha3Hash(password: String) -> (errorCode: Int32, output: [UInt8]) {
let passwordLength = password.lengthOfBytes(using: .utf8)
let input: [UInt8] = [UInt8](password.utf8)
var result: [UInt8] = Array(repeating: 0, count: context.hashLength.rawValue)
var errorCode: Int32
switch context.hashLength {
case .bit224:
errorCode = SHA3_224(&result, input, passwordLength)
case .bit256:
errorCode = SHA3_256(&result, input, passwordLength)
case .bit384:
errorCode = SHA3_384(&result, input, passwordLength)
case .bit512:
errorCode = SHA3_512(&result, input, passwordLength)
}
return (errorCode, result)
}
// MARK: - Hashing
public func hash(password: String) -> CatCryptoResult {
let result = sha3Hash(password: password)
if result.errorCode == 0 {
return CatCryptoResult(raw: result.output)
} else {
return CatCryptoResult(error: CatCryptoError(errorCode: Int(result.errorCode), errorDescription: "Fail"))
}
}
}