Skip to content

Commit

Permalink
Restructing code to use encypt and decypt function in other sdk's
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaurav Goel committed Oct 1, 2024
1 parent 23083bf commit d22336f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 33 deletions.
37 changes: 37 additions & 0 deletions Sources/TorusUtils/Helpers/EncryptionUtils.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import Foundation

#if canImport(curveSecp256k1)
import curveSecp256k1
#endif

public class EncryptionUtils {

public static func decryptNodeData(eciesData: EciesHexOmitCiphertext, ciphertextHex: String, privKey: String) throws -> String {
let eciesOpts = ECIES(
iv: eciesData.iv,
ephemPublicKey: eciesData.ephemPublicKey,
ciphertext: ciphertextHex,
mac: eciesData.mac
)

let decryptedSigBuffer = try decrypt(privateKey: privKey, opts: eciesOpts).hexString
return decryptedSigBuffer
}

public static func decrypt(privateKey: String, opts: ECIES) throws -> Data {
let secret = try SecretKey(hex: privateKey)
var publicKey = opts.ephemPublicKey
if opts.ephemPublicKey.count == 128 { // missing 04 prefix
publicKey = publicKey.add04PrefixUnchecked()
}
let msg = try EncryptedMessage(cipherText: opts.ciphertext, ephemeralPublicKey: PublicKey(hex: publicKey), iv: opts.iv, mac: opts.mac)
let result = try Encryption.decrypt(sk: secret, encrypted: msg)
return result
}

public static func encrypt(publicKey: String, msg: String) throws -> Ecies {
let data = Data(hex: msg)
let curveMsg = try Encryption.encrypt(pk: PublicKey(hex: publicKey), plainText: data)
return try .init(iv: curveMsg.iv(), ephemPublicKey: curveMsg.ephemeralPublicKey().serialize(compressed: false), ciphertext: curveMsg.chipherText(), mac: curveMsg.mac())
}
}
23 changes: 3 additions & 20 deletions Sources/TorusUtils/Helpers/MetadataUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,15 @@ import OSLog

internal class MetadataUtils {
public static func decryptNodeData(eciesData: EciesHexOmitCiphertext, ciphertextHex: String, privKey: String) throws -> String {
let eciesOpts = ECIES(
iv: eciesData.iv,
ephemPublicKey: eciesData.ephemPublicKey,
ciphertext: ciphertextHex,
mac: eciesData.mac
)

let decryptedSigBuffer = try decrypt(privateKey: privKey, opts: eciesOpts).hexString
return decryptedSigBuffer
return try EncryptionUtils.decryptNodeData(eciesData: eciesData, ciphertextHex: ciphertextHex, privKey: privKey)
}

public static func decrypt(privateKey: String, opts: ECIES) throws -> Data {
let secret = try SecretKey(hex: privateKey)
var publicKey = opts.ephemPublicKey
if opts.ephemPublicKey.count == 128 { // missing 04 prefix
publicKey = publicKey.add04PrefixUnchecked()
}
let msg = try EncryptedMessage(cipherText: opts.ciphertext, ephemeralPublicKey: PublicKey(hex: publicKey), iv: opts.iv, mac: opts.mac)
let result = try Encryption.decrypt(sk: secret, encrypted: msg)
return result
return try EncryptionUtils.decrypt(privateKey: privateKey, opts: opts)
}

public static func encrypt(publicKey: String, msg: String) throws -> Ecies {
let data = Data(hex: msg)
let curveMsg = try Encryption.encrypt(pk: PublicKey(hex: publicKey), plainText: data)
return try .init(iv: curveMsg.iv(), ephemPublicKey: curveMsg.ephemeralPublicKey().serialize(compressed: false), ciphertext: curveMsg.chipherText(), mac: curveMsg.mac())
return try EncryptionUtils.encrypt(publicKey: publicKey, msg: msg)
}

internal static func makeUrlRequest(url: String, httpMethod: httpMethod = .post) throws -> URLRequest {
Expand Down
26 changes: 13 additions & 13 deletions Sources/TorusUtils/Interfaces/Common/Ecies.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ protocol EciesProtocol {
var mac: Data { get }
}

internal struct ECIES: Codable {
public struct ECIES: Codable {
let iv: String
let ephemPublicKey: String
let ciphertext: String
let mac: String
let mode: String?

init(iv: String, ephemPublicKey: String, ciphertext: String, mac: String, mode: String? = nil) {
public init(iv: String, ephemPublicKey: String, ciphertext: String, mac: String, mode: String? = nil) {
self.iv = iv
self.ephemPublicKey = ephemPublicKey
self.ciphertext = ciphertext
Expand All @@ -23,77 +23,77 @@ internal struct ECIES: Codable {
}
}

internal struct EciesHex: Codable {
public struct EciesHex: Codable {
let iv: String
let ephemPublicKey: String
let ciphertext: String
let mac: String
let mode: String?

init(iv: String, ephemPublicKey: String, ciphertext: String, mac: String, mode: String?) {
public init(iv: String, ephemPublicKey: String, ciphertext: String, mac: String, mode: String?) {
self.iv = iv
self.ephemPublicKey = ephemPublicKey
self.ciphertext = ciphertext
self.mac = mac
self.mode = mode
}

init(from: Ecies) {
public init(from: Ecies) {
ciphertext = from.ciphertext
iv = from.iv
ephemPublicKey = from.ephemPublicKey
mac = from.mac
mode = "AES256"
}

func omitCiphertext() -> EciesHexOmitCiphertext {
public func omitCiphertext() -> EciesHexOmitCiphertext {
return EciesHexOmitCiphertext(iv: iv, ephemPublicKey: ephemPublicKey, mac: mac, mode: mode)
}
}

internal struct EciesHexOmitCiphertext: Codable {
public struct EciesHexOmitCiphertext: Codable {
var iv: String
var ephemPublicKey: String
var mac: String
var mode: String?

init(iv: String, ephemPublicKey: String, mac: String, mode: String? = nil) {
public init(iv: String, ephemPublicKey: String, mac: String, mode: String? = nil) {
self.iv = iv
self.ephemPublicKey = ephemPublicKey
self.mac = mac
self.mode = mode
}

init(from: ECIES) {
public init(from: ECIES) {
iv = from.iv
ephemPublicKey = from.ephemPublicKey
mac = from.mac
mode = from.mode
}

init(from: Ecies) {
public init(from: Ecies) {
iv = from.iv
ephemPublicKey = from.ephemPublicKey
mac = from.mac
mode = "AES256"
}
}

internal struct Ecies: Codable {
public struct Ecies: Codable {
var iv: String
var ephemPublicKey: String
var ciphertext: String
var mac: String

init(iv: String, ephemPublicKey: String, ciphertext: String, mac: String) {
public init(iv: String, ephemPublicKey: String, ciphertext: String, mac: String) {
self.iv = iv
self.ephemPublicKey = ephemPublicKey
self.ciphertext = ciphertext
self.mac = mac
}
}

internal struct EciesOmitCiphertext {
public struct EciesOmitCiphertext {
var iv: String
var ephemPublicKey: String
var mac: String
Expand Down

0 comments on commit d22336f

Please sign in to comment.