This repository has been archived by the owner on Sep 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
3,054 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Pod::Spec.new do |s| | ||
s.name = "BlueCryptor" | ||
s.version = "1.0.23" | ||
s.summary = "Swift cross-platform crypto library using CommonCrypto/libcrypto via Package Manager." | ||
s.homepage = "https://github.com/IBM-Swift/BlueCryptor" | ||
s.license = { :type => "Apache License, Version 2.0" } | ||
s.author = "IBM" | ||
s.module_name = 'Cryptor' | ||
|
||
s.requires_arc = true | ||
s.ios.deployment_target = "8.0" | ||
s.osx.deployment_target = "10.10" | ||
s.tvos.deployment_target = "9.0" | ||
s.watchos.deployment_target = "2.0" | ||
s.source = { :git => "https://github.com/v57/BlueCryptor.git", :tag => s.version } | ||
s.source_files = "Sources/Cryptor/*.swift" | ||
s.pod_target_xcconfig = { | ||
'SWIFT_VERSION' => '4.2', | ||
} | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,6 @@ | |
|
||
import Foundation | ||
import CryptoSwift | ||
import Cryptor | ||
|
||
|
||
/// Data errors | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
// | ||
// Crypto.swift | ||
// Cryptor | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Foundation | ||
|
||
/// | ||
/// Implements a simplified API for calculating digests over single buffers | ||
/// | ||
public protocol CryptoDigest { | ||
|
||
/// Calculates a message digest | ||
func digest(using algorithm: Digest.Algorithm) -> Self | ||
} | ||
|
||
/// | ||
/// Extension to the CryptoDigest to return the digest appropriate to the selected algorithm. | ||
/// | ||
extension CryptoDigest { | ||
|
||
/// An MD2 digest of this object | ||
public var md2: Self { | ||
return self.digest(using: .md2) | ||
} | ||
|
||
/// An MD4 digest of this object | ||
public var md4: Self { | ||
return self.digest(using: .md4) | ||
} | ||
|
||
/// An MD5 digest of this object | ||
public var md5: Self { | ||
return self.digest(using: .md5) | ||
} | ||
|
||
/// An SHA1 digest of this object | ||
public var sha1: Self { | ||
return self.digest(using: .sha1) | ||
} | ||
|
||
/// An SHA224 digest of this object | ||
public var sha224: Self { | ||
return self.digest(using: .sha224) | ||
} | ||
|
||
/// An SHA256 digest of this object | ||
public var sha256: Self { | ||
return self.digest(using: .sha256) | ||
} | ||
|
||
/// An SHA384 digest of this object | ||
public var sha384: Self { | ||
return self.digest(using: .sha384) | ||
} | ||
|
||
/// An SHA512 digest of this object | ||
public var sha512: Self { | ||
return self.digest(using: .sha512) | ||
} | ||
} | ||
|
||
/// | ||
/// Extension for Data to return an Data object containing the digest. | ||
/// | ||
extension Data: CryptoDigest { | ||
/// | ||
/// Calculates the Message Digest for this data. | ||
/// | ||
/// - Parameter algorithm: The digest algorithm to use | ||
/// | ||
/// - Returns: An `Data` object containing the message digest | ||
/// | ||
public func digest(using algorithm: Digest.Algorithm) -> Data { | ||
|
||
// This force unwrap may look scary but for CommonCrypto this cannot fail. | ||
// The API allows for optionals to support the OpenSSL implementation which can. | ||
return self.withUnsafeBytes() { (buffer: UnsafePointer<UInt8>) -> Data in | ||
|
||
let result = (Digest(using: algorithm).update(from: buffer, byteCount: self.count)?.final())! | ||
let data = type(of: self).init(bytes: result, count: result.count) | ||
return data | ||
} | ||
} | ||
} | ||
|
||
/// | ||
/// Extension for String to return a String containing the digest. | ||
/// | ||
extension String: CryptoDigest { | ||
/// | ||
/// Calculates the Message Digest for this string. | ||
/// The string is converted to raw data using UTF8. | ||
/// | ||
/// - Parameter algorithm: The digest algorithm to use | ||
/// | ||
/// - Returns: A hex string of the calculated digest | ||
/// | ||
public func digest(using algorithm: Digest.Algorithm) -> String { | ||
|
||
// This force unwrap may look scary but for CommonCrypto this cannot fail. | ||
// The API allows for optionals to support the OpenSSL implementation which can. | ||
let result = (Digest(using: algorithm).update(string: self as String)?.final())! | ||
return CryptoUtils.hexString(from: result) | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// | ||
// Cryptor.swift | ||
// Cryptor | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Foundation | ||
|
||
/// | ||
/// Encrypts or decrypts, accumulating result. | ||
/// | ||
/// Useful for small in-memory buffers. | ||
/// | ||
/// For large files or network streams use StreamCryptor. | ||
/// | ||
public class Cryptor: StreamCryptor, Updatable { | ||
|
||
/// Internal accumulator for gathering data from the update() and final() functions. | ||
var accumulator: [UInt8] = [] | ||
|
||
/// | ||
/// Retrieves the encrypted or decrypted data. | ||
/// | ||
///- Returns: the encrypted or decrypted data or nil if an error occured. | ||
/// | ||
public func final() -> [UInt8]? { | ||
|
||
let byteCount = Int(self.getOutputLength(inputByteCount: 0, isFinal: true)) | ||
var dataOut = Array<UInt8>(repeating: 0, count:byteCount) | ||
var dataOutMoved = 0 | ||
(dataOutMoved, self.status) = final(byteArrayOut: &dataOut) | ||
if self.status != .success { | ||
return nil | ||
} | ||
accumulator += dataOut[0..<Int(dataOutMoved)] | ||
return accumulator | ||
} | ||
|
||
/// | ||
/// Upates the accumulated encrypted/decrypted data with the contents | ||
/// of a raw byte buffer. | ||
/// | ||
/// It is not envisaged the users of the framework will need to call this directly. | ||
/// | ||
/// - Returns: this Cryptor object or nil if an error occurs (for optional chaining) | ||
/// | ||
public func update(from buffer: UnsafeRawPointer, byteCount: Int) -> Self? { | ||
|
||
let outputLength = Int(self.getOutputLength(inputByteCount: byteCount, isFinal: false)) | ||
var dataOut = Array<UInt8>(repeating: 0, count:outputLength) | ||
var dataOutMoved = 0 | ||
_ = update(bufferIn: buffer, byteCountIn: byteCount, bufferOut: &dataOut, byteCapacityOut: dataOut.count, byteCountOut: &dataOutMoved) | ||
if self.status != .success { | ||
return nil | ||
} | ||
accumulator += dataOut[0..<Int(dataOutMoved)] | ||
return self | ||
} | ||
|
||
} |
Oops, something went wrong.