Skip to content
This repository has been archived by the owner on Sep 13, 2024. It is now read-only.

Commit

Permalink
Moved BlueCryptor library
Browse files Browse the repository at this point in the history
  • Loading branch information
v57 committed Dec 3, 2018
1 parent 0f6d95f commit c4df379
Show file tree
Hide file tree
Showing 23 changed files with 3,054 additions and 13 deletions.
20 changes: 20 additions & 0 deletions BlueCryptor.podspec
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
1 change: 0 additions & 1 deletion Cartfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ github "mxcl/PromiseKit" ~> 6.0
github "attaswift/BigInt" ~> 3.1
github "krzyzanowskim/CryptoSwift"
github "Boilertalk/secp256k1.swift"
github "v57/BlueCryptor" ~> 1.0
1 change: 0 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ let package = Package(
.package(url: "https://github.com/krzyzanowskim/CryptoSwift.git", from: "0.12.0"),
.package(url: "https://github.com/Boilertalk/secp256k1.swift.git", from: "0.1.1"),
.package(url: "https://github.com/mxcl/PromiseKit.git", from: "6.4.0"),
.package(url: "https://github.com/v57/BlueCryptor.git", from: "1.0.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
Expand Down
2 changes: 1 addition & 1 deletion Sources/Convenience/CryptoExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// Copyright © 2017 Alexander Vlasov. All rights reserved.
//

import Cryptor
//import Cryptor
import Foundation

/**
Expand Down
1 change: 0 additions & 1 deletion Sources/Convenience/Data+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import Foundation
import CryptoSwift
import Cryptor


/// Data errors
Expand Down
4 changes: 2 additions & 2 deletions Sources/Convenience/LibSecp256k1Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ struct SECP256K1 {
try privateKey.checkPrivateKeySize()
var publicKey = secp256k1_pubkey()
let result = privateKey.withUnsafeBytes { (privateKeyPointer: UnsafePointer<UInt8>) in
secp256k1_ec_pubkey_create(context!, UnsafeMutablePointer<secp256k1_pubkey>(&publicKey), privateKeyPointer)
secp256k1_ec_pubkey_create(context!, &publicKey, privateKeyPointer)
}
guard result != 0 else { throw SECP256DataError.cannotExtractPublicKeyFromPrivateKey }
return publicKey
Expand All @@ -248,7 +248,7 @@ struct SECP256K1 {
let keyLen: Int = Int(serializedKey.count)
var publicKey = secp256k1_pubkey()
let result = serializedKey.withUnsafeBytes { (serializedKeyPointer: UnsafePointer<UInt8>) in
secp256k1_ec_pubkey_parse(context!, UnsafeMutablePointer<secp256k1_pubkey>(&publicKey), serializedKeyPointer, keyLen)
secp256k1_ec_pubkey_parse(context!, &publicKey, serializedKeyPointer, keyLen)
}
guard result != 0 else { throw SECP256DataError.cannotParsePublicKey }
return publicKey
Expand Down
119 changes: 119 additions & 0 deletions Sources/Encryption/Cryptor/Crypto.swift
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)

}
}
71 changes: 71 additions & 0 deletions Sources/Encryption/Cryptor/Cryptor.swift
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
}

}
Loading

0 comments on commit c4df379

Please sign in to comment.