-
Notifications
You must be signed in to change notification settings - Fork 73
enhance the functions of ECPublicKey and ECPrivateKey, and add ECDH f… #192
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ library pointycastle.api.ecc; | |
import "dart:typed_data"; | ||
|
||
import "package:pointycastle/api.dart"; | ||
import "package:pointycastle/key_generators/api.dart"; | ||
import "package:pointycastle/src/registry/registry.dart"; | ||
|
||
/// Standard ECC curve description | ||
|
@@ -130,6 +131,27 @@ class ECPrivateKey extends ECAsymmetricKey implements PrivateKey { | |
return (other.parameters == this.parameters) && (other.d == this.d); | ||
} | ||
|
||
/// Restore privatekey from stored hex string | ||
static ECPrivateKey fromStored(String storedPriKey, ECDomainParameters domainParams){ | ||
ECDomainParameters _params = domainParams; | ||
BigInt theD = BigInt.parse(storedPriKey, radix: 16); | ||
return new ECPrivateKey(theD, _params); | ||
} | ||
|
||
/// return Hex string of d, can easily store or transfer on internet | ||
String hexString() => this.d.toRadixString(16); | ||
|
||
/// Generate publickey from private key | ||
ECPublicKey generatePubkey(){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rename to just |
||
var Q = this.parameters.G * this.d; | ||
return new ECPublicKey(Q, this.parameters); | ||
} | ||
|
||
Uint8List generateSecret(ECPublicKey pubkey) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wait is this Shamir's secret sharing? I would certainly not name this /// Create a shared secret using Shamir's secret sharing scheme.
Uint8List getSharedSecret(ECPublicKey other) { |
||
var secret = pubkey.Q * this.d; | ||
return secret.getEncoded(); | ||
} | ||
|
||
int get hashCode { | ||
return parameters.hashCode + d.hashCode; | ||
} | ||
|
@@ -149,6 +171,17 @@ class ECPublicKey extends ECAsymmetricKey implements PublicKey { | |
return (other.parameters == this.parameters) && (other.Q == this.Q); | ||
} | ||
|
||
/// return Uint8List so can easily stored or transfer on internet | ||
Uint8List getEncoded(){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return this.Q.getEncoded(); | ||
} | ||
|
||
/// restore public key from Uint8List from stored string or internet | ||
static ECPublicKey fromStored(Uint8List theQ, ECDomainParameters domainParams){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Why do you do bytes for public key and hex string for private key? Quite inconsistent.. I'd either just have bytes on both or hex on both or both on both. |
||
ECPoint Q = domainParams.curve.decodePoint(theQ); | ||
return new ECPublicKey(Q, domainParams); | ||
} | ||
|
||
int get hashCode { | ||
return parameters.hashCode + Q.hashCode; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you rename this
fromHex
instead? Someone could store a private key in base64 as well, f.e..