-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restructing code to use encypt and decypt function in other sdk's
- Loading branch information
Gaurav Goel
committed
Oct 1, 2024
1 parent
23083bf
commit d22336f
Showing
3 changed files
with
53 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters