Skip to content

Commit

Permalink
chore: some renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
sammous committed Jun 2, 2023
1 parent 3cc5ab4 commit 4de8d2b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 20 deletions.
23 changes: 13 additions & 10 deletions Sources/Xenissuing/SecureSession/SecureSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import Security

protocol Crypto {
func generateRandom(size: Int) throws -> Data
func generateSessionId(sessionKey: Data) throws -> EncryptedMessage
func encrypt(plain: Data, iv: Data, sessionKey: Data) throws -> EncryptedMessage
func generateSessionId(sessionKey: Data) throws -> SecuredSession
func encrypt(plain: Data, iv: Data, sessionKey: Data) throws -> SecuredSession
func decrypt(secret: String, sessionKey: Data, iv: String) throws -> Data
}

/// Encapsulates encrypted message and key used as encryption key.
public struct EncryptedMessage {
public struct SecuredSession {
internal let key: Data
public let sealed: Data
public init(key: Data, sealed: Data) {
Expand All @@ -27,7 +27,7 @@ public struct EncryptedMessage {
public class SecureSession: Crypto {
/// The key provided by Xendit.
let xenditPublicKey: SecKey
var secureSession: EncryptedMessage?
var secureSession: SecuredSession?

/**
Initializes an object with the provided public key data and tag.
Expand Down Expand Up @@ -74,9 +74,12 @@ public class SecureSession: Crypto {
let sKey = try self.generateRandom()
self.secureSession = try self.generateSessionId(sessionKey: sKey)
}


/**
Returns the encrypted session key.
*/
public func getKey() -> Data {
return self.secureSession!.key
return self.secureSession!.sealed
}

public func decryptCardData(secret: String, iv: String) throws -> Data {
Expand Down Expand Up @@ -106,12 +109,12 @@ public class SecureSession: Crypto {
if there was any issue during encryption.
- Returns: The encrypted text
*/
internal func generateSessionId(sessionKey: Data) throws -> EncryptedMessage {
internal func generateSessionId(sessionKey: Data) throws -> SecuredSession {
do {
let sealed = try self.xenditPublicKey.encrypt(
algorithm: .rsaEncryptionOAEPSHA256,
plaintext: sessionKey)
return EncryptedMessage(key: sessionKey, sealed: sealed)
return SecuredSession(key: sessionKey, sealed: sealed)
} catch {
throw XenError.generateSessionIdError("")
}
Expand All @@ -126,13 +129,13 @@ public class SecureSession: Crypto {
if there was any issue during encryption.
- Returns: The encrypted text
*/
public func encrypt(plain: Data, iv _: Data, sessionKey: Data) throws -> EncryptedMessage {
public func encrypt(plain: Data, iv _: Data, sessionKey: Data) throws -> SecuredSession {
do {
let iv = AES.randomIV(32)
let gcm = GCM(iv: iv, mode: .combined)
let aes = try AES(key: sessionKey.bytes, blockMode: gcm, padding: .noPadding)
let sealed = try aes.encrypt(plain.bytes)
return EncryptedMessage(key: sessionKey, sealed: Data(sealed))
return SecuredSession(key: sessionKey, sealed: Data(sealed))
} catch {
throw XenError.encryptionError("")
}
Expand Down
12 changes: 2 additions & 10 deletions Sources/Xenissuing/Xenissuing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,14 @@ import Foundation
@available(macOS 10.15, *)
public enum Xenissuing {
/**
Initializes XenIssuing module.
Create a secure session used to generate a key and decrypt card data.

- Parameters:
- xenditPublicKeyData: Public Key.
- xenditPublicKeyTag: Public Key Tag. If provided, it will try to check first keychain to get the key data.

- Returns: Main module.
- Returns: Secure session object.
*/
// override public init(xenditPublicKeyData: Data, xenditPublicKeyTag: String? = nil) throws {
// do {
// try super.init(xenditPublicKeyData: xenditPublicKeyData, xenditPublicKeyTag: xenditPublicKeyTag)
// } catch {
// throw error
// }
// }

public static func createSecureSession(xenditPublicKeyData: Data, xenditPublicKeyTag: String? = nil) throws -> SecureSession {
let secSession: SecureSession = try SecureSession(xenditPublicKeyData: xenditPublicKeyData, xenditPublicKeyTag: xenditPublicKeyTag)
return secSession
Expand Down
File renamed without changes.
10 changes: 10 additions & 0 deletions Tests/XenissuingTests/XenissuingTests.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import Crypto
import CryptoKit
import CryptoSwift
import Foundation
import Security
import XCTest
@testable import Xenissuing

Expand All @@ -15,4 +20,9 @@ final class XenissuingTests: XCTestCase {
let secureSession = try Xenissuing.createSecureSession(xenditPublicKeyData: Data(base64Encoded: validPublicKey)!)
XCTAssertNotNil(secureSession.secureSession)
}

func testSecureSessionGetKey() throws {
let secureSession = try Xenissuing.createSecureSession(xenditPublicKeyData: Data(base64Encoded: validPublicKey)!)
XCTAssertNotNil(secureSession.getKey())
}
}

0 comments on commit 4de8d2b

Please sign in to comment.