Skip to content

Commit

Permalink
生成 Seed OK;UT OK
Browse files Browse the repository at this point in the history
  • Loading branch information
Cocody committed Jul 25, 2017
1 parent a4ed7cf commit 772cf7b
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 126 deletions.
38 changes: 36 additions & 2 deletions CKMnemonic/CKMnemonic/CKMnemonic/CKMnemonic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,42 @@ class CKMnemonic: NSObject {
return mnemonic.joined(separator: " ")
}

static func deterministicSeedString(from mnemonicString: String, passphrase: String = "", language: CKMnemonicLanguageType) -> String {
return ""
static func deterministicSeedString(from mnemonic: String, passphrase: String = "", language: CKMnemonicLanguageType) -> String {

func normalized(string: String) -> Data? {
guard let data = string.data(using: .utf8, allowLossyConversion: true) else {
return nil
}

guard let dataString = String(data: data, encoding: .utf8) else {
return nil
}

guard let normalizedData = dataString.data(using: .utf8, allowLossyConversion: false) else {
return nil
}
return normalizedData
}

guard let normalizedData = normalized(string: mnemonic) else {
return ""
}

guard let saltData = normalized(string: "mnemonic" + passphrase) else {
return ""
}

let password = normalizedData.bytes
let salt = saltData.bytes

do {
let bytes = try PKCS5.PBKDF2(password: password, salt: salt, iterations: 2048, variant: .sha512).calculate()

return bytes.toHexString()
} catch {
print(error)
return ""
}
}

static func generateMnemonic(strength: Int, language: CKMnemonicLanguageType) throws -> String {
Expand Down
4 changes: 4 additions & 0 deletions CKMnemonic/CKMnemonic/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ class ViewController: UIViewController {
do {
// let mnemonic = try CKMnemonic.generateMnemonic(strength: 128, language: .english)
let mnemonic = try CKMnemonic.mnemonicString(from: "994fe657cc35757e7256812ff2791249", language: .english)
let seed = CKMnemonic.deterministicSeedString(from: mnemonic, passphrase: "Test", language: .english)
let seedNoSec = CKMnemonic.deterministicSeedString(from: mnemonic, language: .english)
print(seed)
print(seedNoSec)
print(mnemonic)
} catch {
print(error)
Expand Down
2 changes: 2 additions & 0 deletions CKMnemonic/CKMnemonicTests/CKMnemonicTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class CKMnemonicTests: XCTestCase {
let selfM = try CKMnemonic.mnemonicString(from: test[0], language: .english)
let m = test[1]
XCTAssertTrue(selfM == m, "计算出的助记词没有通过测试")

// let seed = CKMnemonic.deterministicSeedString(from: selfM, language: .english)
}
}
} catch {
Expand Down
Loading

0 comments on commit 772cf7b

Please sign in to comment.