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

Commit c4df379

Browse files
committed
Moved BlueCryptor library
1 parent 0f6d95f commit c4df379

23 files changed

+3054
-13
lines changed

BlueCryptor.podspec

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Pod::Spec.new do |s|
2+
s.name = "BlueCryptor"
3+
s.version = "1.0.23"
4+
s.summary = "Swift cross-platform crypto library using CommonCrypto/libcrypto via Package Manager."
5+
s.homepage = "https://github.com/IBM-Swift/BlueCryptor"
6+
s.license = { :type => "Apache License, Version 2.0" }
7+
s.author = "IBM"
8+
s.module_name = 'Cryptor'
9+
10+
s.requires_arc = true
11+
s.ios.deployment_target = "8.0"
12+
s.osx.deployment_target = "10.10"
13+
s.tvos.deployment_target = "9.0"
14+
s.watchos.deployment_target = "2.0"
15+
s.source = { :git => "https://github.com/v57/BlueCryptor.git", :tag => s.version }
16+
s.source_files = "Sources/Cryptor/*.swift"
17+
s.pod_target_xcconfig = {
18+
'SWIFT_VERSION' => '4.2',
19+
}
20+
end

Cartfile

-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@ github "mxcl/PromiseKit" ~> 6.0
22
github "attaswift/BigInt" ~> 3.1
33
github "krzyzanowskim/CryptoSwift"
44
github "Boilertalk/secp256k1.swift"
5-
github "v57/BlueCryptor" ~> 1.0

Package.swift

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ let package = Package(
1414
.package(url: "https://github.com/krzyzanowskim/CryptoSwift.git", from: "0.12.0"),
1515
.package(url: "https://github.com/Boilertalk/secp256k1.swift.git", from: "0.1.1"),
1616
.package(url: "https://github.com/mxcl/PromiseKit.git", from: "6.4.0"),
17-
.package(url: "https://github.com/v57/BlueCryptor.git", from: "1.0.0"),
1817
],
1918
targets: [
2019
// Targets are the basic building blocks of a package. A target can define a module or a test suite.

Sources/Convenience/CryptoExtensions.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// Copyright © 2017 Alexander Vlasov. All rights reserved.
77
//
88

9-
import Cryptor
9+
//import Cryptor
1010
import Foundation
1111

1212
/**

Sources/Convenience/Data+Extension.swift

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import Foundation
1010
import CryptoSwift
11-
import Cryptor
1211

1312

1413
/// Data errors

Sources/Convenience/LibSecp256k1Extension.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ struct SECP256K1 {
222222
try privateKey.checkPrivateKeySize()
223223
var publicKey = secp256k1_pubkey()
224224
let result = privateKey.withUnsafeBytes { (privateKeyPointer: UnsafePointer<UInt8>) in
225-
secp256k1_ec_pubkey_create(context!, UnsafeMutablePointer<secp256k1_pubkey>(&publicKey), privateKeyPointer)
225+
secp256k1_ec_pubkey_create(context!, &publicKey, privateKeyPointer)
226226
}
227227
guard result != 0 else { throw SECP256DataError.cannotExtractPublicKeyFromPrivateKey }
228228
return publicKey
@@ -248,7 +248,7 @@ struct SECP256K1 {
248248
let keyLen: Int = Int(serializedKey.count)
249249
var publicKey = secp256k1_pubkey()
250250
let result = serializedKey.withUnsafeBytes { (serializedKeyPointer: UnsafePointer<UInt8>) in
251-
secp256k1_ec_pubkey_parse(context!, UnsafeMutablePointer<secp256k1_pubkey>(&publicKey), serializedKeyPointer, keyLen)
251+
secp256k1_ec_pubkey_parse(context!, &publicKey, serializedKeyPointer, keyLen)
252252
}
253253
guard result != 0 else { throw SECP256DataError.cannotParsePublicKey }
254254
return publicKey
+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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+
}
+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//
2+
// Cryptor.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+
/// Encrypts or decrypts, accumulating result.
22+
///
23+
/// Useful for small in-memory buffers.
24+
///
25+
/// For large files or network streams use StreamCryptor.
26+
///
27+
public class Cryptor: StreamCryptor, Updatable {
28+
29+
/// Internal accumulator for gathering data from the update() and final() functions.
30+
var accumulator: [UInt8] = []
31+
32+
///
33+
/// Retrieves the encrypted or decrypted data.
34+
///
35+
///- Returns: the encrypted or decrypted data or nil if an error occured.
36+
///
37+
public func final() -> [UInt8]? {
38+
39+
let byteCount = Int(self.getOutputLength(inputByteCount: 0, isFinal: true))
40+
var dataOut = Array<UInt8>(repeating: 0, count:byteCount)
41+
var dataOutMoved = 0
42+
(dataOutMoved, self.status) = final(byteArrayOut: &dataOut)
43+
if self.status != .success {
44+
return nil
45+
}
46+
accumulator += dataOut[0..<Int(dataOutMoved)]
47+
return accumulator
48+
}
49+
50+
///
51+
/// Upates the accumulated encrypted/decrypted data with the contents
52+
/// of a raw byte buffer.
53+
///
54+
/// It is not envisaged the users of the framework will need to call this directly.
55+
///
56+
/// - Returns: this Cryptor object or nil if an error occurs (for optional chaining)
57+
///
58+
public func update(from buffer: UnsafeRawPointer, byteCount: Int) -> Self? {
59+
60+
let outputLength = Int(self.getOutputLength(inputByteCount: byteCount, isFinal: false))
61+
var dataOut = Array<UInt8>(repeating: 0, count:outputLength)
62+
var dataOutMoved = 0
63+
_ = update(bufferIn: buffer, byteCountIn: byteCount, bufferOut: &dataOut, byteCapacityOut: dataOut.count, byteCountOut: &dataOutMoved)
64+
if self.status != .success {
65+
return nil
66+
}
67+
accumulator += dataOut[0..<Int(dataOutMoved)]
68+
return self
69+
}
70+
71+
}

0 commit comments

Comments
 (0)