Skip to content

Commit

Permalink
add encryption and decryption methods
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnzhou committed Apr 28, 2024
1 parent f47bc70 commit ec045e4
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/johnnzhou/lcl-k1.git", branch: "main"),
.package(url: "https://github.com/johnnzhou/swift-crypto.git", branch: "main"),
.package(url: "https://github.com/johnnzhou/swift-crypto.git", branch: "main")
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
Expand Down
2 changes: 1 addition & 1 deletion Sources/LCLPingAuth/Auth/Authenticate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,6 @@ public func validate(credential: Data) throws -> ValidationResult {

h_pkr = digest(data: outputData, algorithm: .SHA256)
outputData.removeAll()

return ValidationResult(R: r, skT: skT, hPKR: h_pkr)
}
2 changes: 0 additions & 2 deletions Sources/LCLPingAuth/Cryptography/ECDSA.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ public class ECDSA {
}
}


extension K1.ECDSA.Signature {
/// The data representation of signature in bytes
var toData: Data {
Expand All @@ -104,4 +103,3 @@ extension K1.ECDSA.Signature {
return Data(byteBuffer)
}
}

6 changes: 0 additions & 6 deletions Sources/LCLPingAuth/Models.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ struct Keys: Decodable {
var sigmaT: String
var skT: String
var pk_a: String

init(sigmaT: String, skT: String, pk_a: String) {
self.sigmaT = sigmaT
self.skT = skT
self.pk_a = pk_a
}
}

extension Keys {
Expand Down
6 changes: 3 additions & 3 deletions Sources/LCLPingAuth/Utils/Data+LCLPingAuth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ private func htoi(_ value: UInt8) throws -> UInt8 {
}

extension Data {

/// Initialize the `Data` from the hex string value
init(hexString: String) throws {
self.init()
Expand All @@ -52,15 +52,15 @@ extension Data {
}

extension ByteArray {

/// The `Data` representation of the byte array
var toData: Data {
return Data(self)
}
}

extension Data {

/// The hex String representation of the given `Data`
var hex: String {
reduce("") { $0 + String(format: "%02hhx", $1) }
Expand Down
34 changes: 30 additions & 4 deletions Sources/LCLPingAuth/Utils/Security+LCLPingAuth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,21 @@ public func generateSecureRandomBytes(count: Int) -> Result<Data, LCLPingAuthErr
let res = withUnsafeMutableBytes(of: &bytes) { buffer in
return getentropy(buffer.baseAddress, count)
}

if res == -1 {
// TODO: check errno for detailed error
return .failure(.keyGenerationFailed)
}

return .success(Data(bytes))
#endif
}

extension Digest {

/// the bytes representation of the `Digest` data
var bytes: [UInt8] { Array(makeIterator()) }

/// the `Data` representation of the `Digest` data
var data: Data { Data(bytes) }
}
Expand Down Expand Up @@ -89,3 +89,29 @@ public func digest(data: Data, algorithm: HashAlgorithm) -> Data {
}
}

/**
Encrypt the plaintext using the given symmetric key

- Parameters:
- plainText: the plaintext data to be encrypted
- key: the symmetric key that will be used for encryption

- Returns: the encrypted data
*/
public func encrypt(plainText: Data, key: SymmetricKey) throws -> Data {
let box = try AES.GCM.seal(plainText, using: key)
return box.combined!
}

/**
Decrypt the cipher data using the given symmetric key

- Parameters:
- cipher: the cipher text to be decrypted
- key: the symmetric key that will be used for decryption
- Returns: the decrypted data
*/
public func decrypt(cipher: Data, key: SymmetricKey) throws -> Data {
let box = try AES.GCM.SealedBox(combined: cipher)
return try AES.GCM.open(box, using: key)
}

0 comments on commit ec045e4

Please sign in to comment.