Skip to content

Instantly share code, notes, and snippets.

@starius
Created August 7, 2019 02:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save starius/5ea09b0fe810fc727f823d504a2e628e to your computer and use it in GitHub Desktop.
Save starius/5ea09b0fe810fc727f823d504a2e628e to your computer and use it in GitHub Desktop.
package main
import (
"crypto/aes"
"crypto/rand"
"crypto/sha256"
"fmt"
"hash"
"github.com/dchest/wots"
)
type AESChainFunc struct {
state []byte
}
func (a *AESChainFunc) Write(p []byte) (n int, err error) {
aesCipher, err := aes.NewCipher(p)
if err != nil {
return 0, err
}
buffer := make([]byte, 16)
aesCipher.Encrypt(buffer, buffer)
if a.state != nil {
panic("multiple writes")
}
a.state = buffer
return len(p), nil
}
func (a *AESChainFunc) Sum(b []byte) []byte {
return append(b, a.state...)
}
func (a *AESChainFunc) Reset() {
a.state = nil
}
func (a *AESChainFunc) Size() int {
return 16
}
func (a *AESChainFunc) BlockSize() int {
return 16
}
func main() {
chainNew := func() hash.Hash {
return &AESChainFunc{}
}
wotssha256 := wots.NewScheme2(sha256.New, chainNew, rand.Reader)
privateKey, publicKey, err := wotssha256.GenerateKeyPair()
if err != nil {
panic("key generation failed")
}
fmt.Println("PrivateKeySize: ", len(privateKey), wotssha256.PrivateKeySize())
fmt.Println("PublicKeySize: ", len(publicKey), wotssha256.PublicKeySize())
message := []byte("stuff to be signed")
signature, err := wotssha256.Sign(privateKey, message) // => 576-byte signature
if err != nil {
panic("signature calculation failed")
}
fmt.Println("SignatureSize: ", len(signature), wotssha256.SignatureSize())
if wotssha256.Verify(publicKey, message, signature) {
fmt.Println("verification succeeded")
} else {
fmt.Println("verification failed")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment