Skip to content

Commit

Permalink
Merge pull request #116 from ArcBlock/update-protobuf
Browse files Browse the repository at this point in the history
fix: convert keypair to pem
  • Loading branch information
karthuszY authored Feb 21, 2023
2 parents 224a3b6 + 140bb23 commit e956e57
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .jazzy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ github_url: https://github.com/ArcBlock/arcblock-ios-sdk
github_file_prefix: https://github.com/ArcBlock/arcblock-ios-sdk/tree/master
exclude:
- ArcBlockSDK/ABSDKCoreKit/Network/ABSDKPagination.swift
module_version: 0.11.39
module_version: 0.11.40
2 changes: 1 addition & 1 deletion ArcBlockSDK.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'ArcBlockSDK'
s.version = '0.11.39'
s.version = '0.11.40'
s.summary = 'Used to integrate iOS apps with ArcBlock Platform.'

# This description is used to generate tags and improve search results.
Expand Down
24 changes: 21 additions & 3 deletions ArcBlockSDK/ABSDKCoreKit/RSAUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import Foundation
import Security

public class RSAUtils {
public static func generateKeyPair(_ length: Int) -> (pk: Data, sk: Data)? {
public static func generateKeyPair() -> (pk: Data, sk: Data)? {
let attributes: [CFString: Any] = [
kSecAttrType: kSecAttrKeyTypeRSA,
kSecAttrKeySizeInBits: 1024,
Expand Down Expand Up @@ -53,8 +53,26 @@ public class RSAUtils {
return (cfPKData as Data , cfSKData as Data)
}
/// ASN1处理过的Pk,可以导出给其他平台使用
public static func exportASN1Pk(data: Data) -> String {
return RSAPublicKeyExporter().toSubjectPublicKeyInfo(data).base64EncodedString()
public static func exportASN1Pk(data: Data) -> Data {
return RSAPublicKeyExporter().toSubjectPublicKeyInfo(data)
}

public static func exportPemB58BtcASN1Pk(data: Data) -> String? {
let base64Pk = RSAPublicKeyExporter().toSubjectPublicKeyInfo(data).base64EncodedString(options: .lineLength64Characters)
let pemBase64Pk = "-----BEGIN PUBLIC KEY-----" + "\n" + base64Pk + "\n" + "-----END PUBLIC KEY-----"
guard let pemData = pemBase64Pk.data(using: .utf8) else {
return nil
}
return pemData.multibaseEncodedString(inBase: .base58BTC)
}

public static func exportPemB58BtcSk(data: Data) -> String? {
let base64Sk = data.base64EncodedString(options: .lineLength64Characters)
let pemBase64Pk = "-----BEGIN PRIVATE KEY-----" + "\n" + base64Sk + "\n" + "-----END PRIVATE KEY-----"
guard let pemData = pemBase64Pk.data(using: .utf8) else {
return nil
}
return pemData.multibaseEncodedString(inBase: .base58BTC)
}

public static func decodeSecKeyFromBase64(encodedKey: String, isPrivate: Bool = false, keySzie: Int = 1024) -> SecKey? {
Expand Down
19 changes: 4 additions & 15 deletions ArcBlockSDKTests/RSAUtilsSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,10 @@ class RSAUtilsSpec: QuickSpec {
describe("RSAUtilsSpec") {
it("works", closure: {

// if let keyPair = RSAUtils.generateKeyPair(1024) {
// print(keyPair.sk.base64EncodedString())
// print(RSAUtils.exportASN1Pk(data: keyPair.pk))
//
// let encryted = RSAUtils.encryptString2B58Btc(abcdOriginString, publicKey: keyPair.pk.base64EncodedString())
// let decrypted = RSAUtils.decryptB58Btc2String(encryted!, privateKey: keyPair.sk.base64EncodedString())
// print(encryted)
// print(decrypted)
//
// let emptyEncryted = RSAUtils.encryptString2B58Btc(emptyOriginString, publicKey: keyPair.pk.base64EncodedString())
// let emptyDecrypted = RSAUtils.decryptB58Btc2String(emptyEncryted!, privateKey: keyPair.sk.base64EncodedString())
// print(emptyEncryted)
// print(emptyDecrypted)
//
// }
if let keyPair = RSAUtils.generateKeyPair() {
print(RSAUtils.exportPemB58BtcASN1Pk(data: keyPair.pk))
print(RSAUtils.exportPemB58BtcSk(data: keyPair.sk))
}

let encryted = RSAUtils.encryptString2B58Btc(abcdOriginString, publicKey: androidPKPEM)
let decrypted = RSAUtils.decryptB58Btc2String(encryted!, privateKey: androidSKPEM)
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 0.11.40 (February 21, 2023)
- fix pem keypair
- Merge pull request #115 from ArcBlock/update-protobuf
- Merge pull request #114 from ArcBlock/update-protobuf
- Merge pull request #113 from ArcBlock/base58-fix
- Merge pull request #112 from ArcBlock/base58-fix

## 0.11.39 (February 21, 2023)


Expand Down
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.11.39
0.11.40

0 comments on commit e956e57

Please sign in to comment.