-
-
Notifications
You must be signed in to change notification settings - Fork 272
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
174 additions
and
5 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
group = "me.leon.tools" | ||
version = "1.12.4.beta03" | ||
version = "1.12.4.beta04" | ||
|
||
plugins { | ||
application | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package me.leon.ext.crypto | ||
|
||
import java.math.BigInteger | ||
import me.leon.ext.toHex | ||
import org.bouncycastle.asn1.sec.SECNamedCurves | ||
import org.bouncycastle.crypto.ec.CustomNamedCurves | ||
import org.bouncycastle.math.ec.ECCurve | ||
|
||
val allCurves = | ||
(CustomNamedCurves.getNames().toList().map { it.toString() } + | ||
SECNamedCurves.getNames().toList().map { it.toString() }) | ||
.distinct() | ||
.sorted() | ||
|
||
val String.curve: ECCurve | ||
get() = (CustomNamedCurves.getByName(this) ?: SECNamedCurves.getByName(this)).curve | ||
|
||
fun String.curveMultiply(x: BigInteger, y: BigInteger, k: BigInteger): Pair<String, String> { | ||
with(curve.createPoint(x, y).multiply(k).getEncoded(false).toHex()) { | ||
return Pair(substring(2, 66), substring(66)) | ||
} | ||
} | ||
|
||
fun String.curveAdd( | ||
x: BigInteger, | ||
y: BigInteger, | ||
x2: BigInteger, | ||
y2: BigInteger | ||
): Pair<String, String> { | ||
with(curve) { | ||
val hex = createPoint(x, y).add(createPoint(x2, y2)).getEncoded(false).toHex() | ||
return Pair(hex.substring(2, 66), hex.substring(66)) | ||
} | ||
} | ||
|
||
fun String.curveSubtract( | ||
x: BigInteger, | ||
y: BigInteger, | ||
x2: BigInteger, | ||
y2: BigInteger | ||
): Pair<String, String> { | ||
with(curve) { | ||
val hex = createPoint(x, y).subtract(createPoint(x2, y2)).getEncoded(false).toHex() | ||
return Pair(hex.substring(2, 66), hex.substring(66)) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package me.leon.view | ||
|
||
import javafx.beans.property.SimpleBooleanProperty | ||
import javafx.beans.property.SimpleStringProperty | ||
import javafx.geometry.Pos | ||
import javafx.scene.control.RadioButton | ||
import javafx.scene.control.TextField | ||
import me.leon.ext.DEFAULT_SPACING | ||
import me.leon.ext.cast | ||
import me.leon.ext.crypto.* | ||
import tornadofx.* | ||
|
||
class ECCurveCalculator : View("ECCurveCalculator") { | ||
private val selectedCurve = SimpleStringProperty(allCurves.last()) | ||
private val isShowY2 = SimpleBooleanProperty(false) | ||
private var tfX1: TextField by singleAssign() | ||
private var tfY1: TextField by singleAssign() | ||
private var tfX2: TextField by singleAssign() | ||
private var tfY2: TextField by singleAssign() | ||
private var tfX: TextField by singleAssign() | ||
private var tfY: TextField by singleAssign() | ||
private var fPoint2: Field by singleAssign() | ||
private var isMultiply = false | ||
private var eccMethod = "multiply" | ||
private var radix = 16 | ||
|
||
override val root = form { | ||
fieldset { | ||
field("x1:") { tfX1 = textfield() } | ||
field("y1:") { tfY1 = textfield() } | ||
} | ||
fieldset { | ||
fPoint2 = field("k:") { tfX2 = textfield() } | ||
field("y2:") { | ||
tfY2 = textfield() | ||
visibleWhen(isShowY2) | ||
} | ||
} | ||
|
||
hbox { | ||
alignment = Pos.CENTER | ||
spacing = DEFAULT_SPACING | ||
togglegroup { | ||
radiobutton("add") | ||
radiobutton("subtract") | ||
radiobutton("multiply") { isSelected = true } | ||
selectedToggleProperty().addListener { _, _, newValue -> | ||
isMultiply = | ||
newValue.cast<RadioButton>().text.also { eccMethod = it } == "multiply" | ||
fPoint2.text = if (isMultiply) "k:" else "y2:" | ||
isShowY2.value = !isMultiply | ||
} | ||
} | ||
combobox(selectedCurve, allCurves) | ||
button(messages["run"]) { action { calculate() } } | ||
} | ||
fieldset { | ||
field("x:") { tfX = textfield() } | ||
field("y:") { tfY = textfield() } | ||
} | ||
} | ||
|
||
private fun calculate() { | ||
runAsync { | ||
run { | ||
when (eccMethod) { | ||
"add" -> | ||
selectedCurve | ||
.get() | ||
.curveAdd( | ||
tfX1.text.also { println(it) }.toBigInteger(radix), | ||
tfY1.text.toBigInteger(radix), | ||
tfX2.text.toBigInteger(radix), | ||
tfY2.text.toBigInteger(radix) | ||
) | ||
"subtract" -> | ||
selectedCurve | ||
.get() | ||
.curveSubtract( | ||
tfX1.text.toBigInteger(radix), | ||
tfY1.text.toBigInteger(radix), | ||
tfX2.text.toBigInteger(radix), | ||
tfY2.text.toBigInteger(radix) | ||
) | ||
"multiply" -> | ||
selectedCurve | ||
.get() | ||
.curveMultiply( | ||
tfX1.text.toBigInteger(radix), | ||
tfY1.text.toBigInteger(radix), | ||
tfX2.text.toBigInteger(radix) | ||
) | ||
else -> throw IllegalArgumentException("Unknown method: $eccMethod") | ||
} | ||
} | ||
} ui | ||
{ | ||
tfX.text = it.first.uppercase() | ||
tfY.text = it.second.uppercase() | ||
} | ||
} | ||
} |
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