Elliptic Curve Digital Signature Algorithm: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (Move TOC to correct position. Modify some markup which caused bogus TOC entries.) |
(Added Go) |
||
Line 850: | Line 850: | ||
Valid |
Valid |
||
_____ |
_____ |
||
</pre> |
|||
=={{header|Go}}== |
|||
Since Go has an ECDSA package in its standard library which uses 'big integers', we use that rather than translating one of the reference implementations for a 'toy' version into Go. |
|||
<lang go>package main |
|||
import ( |
|||
"crypto/ecdsa" |
|||
"crypto/elliptic" |
|||
"crypto/rand" |
|||
"crypto/sha256" |
|||
"encoding/binary" |
|||
"fmt" |
|||
"log" |
|||
) |
|||
func check(err error) { |
|||
if err != nil { |
|||
log.Fatal(err) |
|||
} |
|||
} |
|||
func main() { |
|||
priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) |
|||
check(err) |
|||
fmt.Println("Private key:\nD:", priv.D) |
|||
pub := priv.Public().(*ecdsa.PublicKey) |
|||
fmt.Println("\nPublic key:") |
|||
fmt.Println("X:", pub.X) |
|||
fmt.Println("Y:", pub.Y) |
|||
msg := "Rosetta Code" |
|||
fmt.Println("\nMessage:", msg) |
|||
hash := sha256.Sum256([]byte(msg)) // as [32]byte |
|||
hexHash := fmt.Sprintf("0x%x", binary.BigEndian.Uint32(hash[:])) |
|||
fmt.Println("Hash :", hexHash) |
|||
r, s, err := ecdsa.Sign(rand.Reader, priv, hash[:]) |
|||
check(err) |
|||
fmt.Println("\nSignature:") |
|||
fmt.Println("R:", r) |
|||
fmt.Println("S:", s) |
|||
valid := ecdsa.Verify(&priv.PublicKey, hash[:], r, s) |
|||
fmt.Println("\nSignature verified:", valid) |
|||
}</lang> |
|||
{{out}} |
|||
Sample run: |
|||
<pre> |
|||
Private key: |
|||
D: 25700608762903774973512323993645267346590725880891580901973011512673451968935 |
|||
Public key: |
|||
X: 37298454876588653961191059192981094503652951300904260069480867699946371240473 |
|||
Y: 69073688506493709421315518164229531832022167466292360349457318041854718641652 |
|||
Message: Rosetta Code |
|||
Hash : 0xe6f9ed0d |
|||
Signature: |
|||
R: 91827099055706804696234859308003894767808769875556550819128270941615405955877 |
|||
S: 20295707309473352071389945163735458699476300346398176659149368970668313772860 |
|||
Signature verified: true |
|||
</pre> |
</pre> |