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
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