88 lines
2 KiB
Go
88 lines
2 KiB
Go
|
|
||
|
// edwardsKeys provides functions to generate ed25519 keys and verify ed25519 signatures
|
||
|
// Identity keys and credit account keys are edwardsKeys
|
||
|
|
||
|
package edwardsKeys
|
||
|
|
||
|
|
||
|
import "seekia/internal/encoding"
|
||
|
|
||
|
import goEd25519 "crypto/ed25519"
|
||
|
|
||
|
|
||
|
func VerifyPublicKeyHex(publicKeyHex string)bool{
|
||
|
|
||
|
publicKeyBytes, err := encoding.DecodeHexStringToBytes(publicKeyHex)
|
||
|
if (err != nil) {
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
if (len(publicKeyBytes) != 32) {
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
return true
|
||
|
}
|
||
|
|
||
|
// This function signs content with provided private key
|
||
|
func CreateSignature(privateKey [64]byte, contentHash [32]byte)[64]byte{
|
||
|
|
||
|
signature := goEd25519.Sign(privateKey[:], contentHash[:])
|
||
|
|
||
|
if (len(signature) != 64){
|
||
|
panic("goEd25519.Sign returning invalid signatureLength")
|
||
|
}
|
||
|
|
||
|
signatureArray := [64]byte(signature)
|
||
|
|
||
|
return signatureArray
|
||
|
}
|
||
|
|
||
|
//Outputs:
|
||
|
// -bool: Signature is valid
|
||
|
func VerifySignature(publicKey [32]byte, signature [64]byte, contentHash [32]byte)bool{
|
||
|
|
||
|
isValid := goEd25519.Verify(publicKey[:], contentHash[:], signature[:])
|
||
|
|
||
|
return isValid
|
||
|
}
|
||
|
|
||
|
func GetNewRandomPublicAndPrivateEdwardsKeys()([32]byte, [64]byte, error){
|
||
|
|
||
|
publicKeyObject, privateKeyObject, err := goEd25519.GenerateKey(nil)
|
||
|
if (err != nil) { return [32]byte{}, [64]byte{}, err }
|
||
|
|
||
|
publicKeyArray := [32]byte(publicKeyObject)
|
||
|
privateKeyArray := [64]byte(privateKeyObject)
|
||
|
|
||
|
return publicKeyArray, privateKeyArray, nil
|
||
|
}
|
||
|
|
||
|
//Outputs:
|
||
|
// -[32]byte: Public key
|
||
|
// -[64]byte: Private key
|
||
|
// -error
|
||
|
func GetSeededEdwardsPublicAndPrivateKeys(seedBytes [32]byte)([32]byte, [64]byte){
|
||
|
|
||
|
privateKeyObject := goEd25519.NewKeyFromSeed(seedBytes[:])
|
||
|
|
||
|
goPublicKeyObject := privateKeyObject.Public()
|
||
|
|
||
|
publicKeyObject := goPublicKeyObject.(goEd25519.PublicKey)
|
||
|
|
||
|
if (len(publicKeyObject) != 32){
|
||
|
panic("publicKeyObject is not 32 bytes long.")
|
||
|
}
|
||
|
if (len(privateKeyObject) != 64){
|
||
|
panic("privateKeyObject is not 64 bytes long.")
|
||
|
}
|
||
|
|
||
|
publicKeyArray := [32]byte(publicKeyObject)
|
||
|
privateKeyArray := [64]byte(privateKeyObject)
|
||
|
|
||
|
return publicKeyArray, privateKeyArray
|
||
|
}
|
||
|
|
||
|
|
||
|
|