|
| 1 | +// |
| 2 | +// Crypto.swift |
| 3 | +// Cryptor |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | +// |
| 17 | + |
| 18 | +import Foundation |
| 19 | + |
| 20 | +/// |
| 21 | +/// Implements a simplified API for calculating digests over single buffers |
| 22 | +/// |
| 23 | +public protocol CryptoDigest { |
| 24 | + |
| 25 | + /// Calculates a message digest |
| 26 | + func digest(using algorithm: Digest.Algorithm) -> Self |
| 27 | +} |
| 28 | + |
| 29 | +/// |
| 30 | +/// Extension to the CryptoDigest to return the digest appropriate to the selected algorithm. |
| 31 | +/// |
| 32 | +extension CryptoDigest { |
| 33 | + |
| 34 | + /// An MD2 digest of this object |
| 35 | + public var md2: Self { |
| 36 | + return self.digest(using: .md2) |
| 37 | + } |
| 38 | + |
| 39 | + /// An MD4 digest of this object |
| 40 | + public var md4: Self { |
| 41 | + return self.digest(using: .md4) |
| 42 | + } |
| 43 | + |
| 44 | + /// An MD5 digest of this object |
| 45 | + public var md5: Self { |
| 46 | + return self.digest(using: .md5) |
| 47 | + } |
| 48 | + |
| 49 | + /// An SHA1 digest of this object |
| 50 | + public var sha1: Self { |
| 51 | + return self.digest(using: .sha1) |
| 52 | + } |
| 53 | + |
| 54 | + /// An SHA224 digest of this object |
| 55 | + public var sha224: Self { |
| 56 | + return self.digest(using: .sha224) |
| 57 | + } |
| 58 | + |
| 59 | + /// An SHA256 digest of this object |
| 60 | + public var sha256: Self { |
| 61 | + return self.digest(using: .sha256) |
| 62 | + } |
| 63 | + |
| 64 | + /// An SHA384 digest of this object |
| 65 | + public var sha384: Self { |
| 66 | + return self.digest(using: .sha384) |
| 67 | + } |
| 68 | + |
| 69 | + /// An SHA512 digest of this object |
| 70 | + public var sha512: Self { |
| 71 | + return self.digest(using: .sha512) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +/// |
| 76 | +/// Extension for Data to return an Data object containing the digest. |
| 77 | +/// |
| 78 | +extension Data: CryptoDigest { |
| 79 | + /// |
| 80 | + /// Calculates the Message Digest for this data. |
| 81 | + /// |
| 82 | + /// - Parameter algorithm: The digest algorithm to use |
| 83 | + /// |
| 84 | + /// - Returns: An `Data` object containing the message digest |
| 85 | + /// |
| 86 | + public func digest(using algorithm: Digest.Algorithm) -> Data { |
| 87 | + |
| 88 | + // This force unwrap may look scary but for CommonCrypto this cannot fail. |
| 89 | + // The API allows for optionals to support the OpenSSL implementation which can. |
| 90 | + return self.withUnsafeBytes() { (buffer: UnsafePointer<UInt8>) -> Data in |
| 91 | + |
| 92 | + let result = (Digest(using: algorithm).update(from: buffer, byteCount: self.count)?.final())! |
| 93 | + let data = type(of: self).init(bytes: result, count: result.count) |
| 94 | + return data |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +/// |
| 100 | +/// Extension for String to return a String containing the digest. |
| 101 | +/// |
| 102 | +extension String: CryptoDigest { |
| 103 | + /// |
| 104 | + /// Calculates the Message Digest for this string. |
| 105 | + /// The string is converted to raw data using UTF8. |
| 106 | + /// |
| 107 | + /// - Parameter algorithm: The digest algorithm to use |
| 108 | + /// |
| 109 | + /// - Returns: A hex string of the calculated digest |
| 110 | + /// |
| 111 | + public func digest(using algorithm: Digest.Algorithm) -> String { |
| 112 | + |
| 113 | + // This force unwrap may look scary but for CommonCrypto this cannot fail. |
| 114 | + // The API allows for optionals to support the OpenSSL implementation which can. |
| 115 | + let result = (Digest(using: algorithm).update(string: self as String)?.final())! |
| 116 | + return CryptoUtils.hexString(from: result) |
| 117 | + |
| 118 | + } |
| 119 | +} |
0 commit comments