Last active
January 23, 2021 15:07
-
-
Save taimoorgit/4ecc739098072a277d95336df25b7f6a to your computer and use it in GitHub Desktop.
Keypair generator and signature verifier for liboqs-go
This file contains hidden or 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
// signature Go example | |
package main | |
import ( | |
"fmt" | |
"log" | |
"github.com/open-quantum-safe/liboqs-go/oqs" | |
) | |
func main() { | |
fmt.Println("Enabled signatures:") | |
fmt.Println(oqs.EnabledSigs()) | |
sigName := "Falcon-512" | |
signer := oqs.Signature{} | |
defer signer.Clean() // clean up even in case of panic | |
if err := signer.Init(sigName, nil); err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println("\nSignature details:") | |
fmt.Println(signer.Details()) | |
msg := []byte("This is the message to sign") | |
pubKey, err := signer.GenerateKeyPair() | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Printf("\nSigner public key:\n% X ... % X\n", pubKey[0:8], | |
pubKey[len(pubKey)-8:]) | |
fmt.Println(len(pubKey)) | |
// Private key | |
secKey := signer.ExportSecretKey() | |
fmt.Printf("\nSigner secret key:\n% X ... % X\n", secKey[0:8], | |
secKey[len(secKey)-8:]) | |
fmt.Println(len(secKey)) | |
signature, _ := signer.Sign(msg) | |
fmt.Printf("\nSignature:\n% X ... % X\n", signature[0:8], | |
signature[len(signature)-8:]) | |
fmt.Print(len(signature)) | |
verifier := oqs.Signature{} | |
defer verifier.Clean() // clean up even in case of panic | |
if err := verifier.Init(sigName, nil); err != nil { | |
log.Fatal(err) | |
} | |
isValid, err := verifier.Verify(msg, signature, pubKey) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println("\nValid signature?", isValid) | |
fmt.Println() | |
fmt.Println("Private key bytes:") | |
fmt.Printf("[]byte{") | |
for idx, key := range secKey { | |
if idx == len(secKey)-1 { | |
fmt.Printf("%d", key); | |
} else { | |
fmt.Printf("%d, ", key) | |
} | |
} | |
fmt.Printf("}\n") | |
fmt.Println() | |
fmt.Println("Public key bytes:") | |
fmt.Printf("[]byte{") | |
for idx, key := range pubKey { | |
if idx == len(pubKey)-1 { | |
fmt.Printf("%d", key); | |
} else { | |
fmt.Printf("%d, ", key) | |
} | |
} | |
fmt.Printf("}\n") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment