Skip to content

Commit

Permalink
fix receiver names
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg committed Aug 17, 2020
1 parent fb108dd commit d3fef3d
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 58 deletions.
14 changes: 7 additions & 7 deletions algo_eddsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@ type edDSAAlg struct {
privateKey ed25519.PrivateKey
}

func (h edDSAAlg) Algorithm() Algorithm {
return h.alg
func (ed edDSAAlg) Algorithm() Algorithm {
return ed.alg
}

func (h edDSAAlg) SignSize() int {
func (ed edDSAAlg) SignSize() int {
return ed25519.SignatureSize
}

func (h edDSAAlg) Sign(payload []byte) ([]byte, error) {
return ed25519.Sign(h.privateKey, payload), nil
func (ed edDSAAlg) Sign(payload []byte) ([]byte, error) {
return ed25519.Sign(ed.privateKey, payload), nil
}

func (h edDSAAlg) Verify(payload, signature []byte) error {
if !ed25519.Verify(h.publicKey, payload, signature) {
func (ed edDSAAlg) Verify(payload, signature []byte) error {
if !ed25519.Verify(ed.publicKey, payload, signature) {
return ErrInvalidSignature
}
return nil
Expand Down
32 changes: 16 additions & 16 deletions algo_es.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,26 +65,26 @@ type esAlg struct {
curveBits int
}

func (h esAlg) Algorithm() Algorithm {
return h.alg
func (es esAlg) Algorithm() Algorithm {
return es.alg
}

func (h esAlg) SignSize() int {
return (h.privateKey.Curve.Params().BitSize + 7) / 4
func (es esAlg) SignSize() int {
return (es.privateKey.Curve.Params().BitSize + 7) / 4
}

func (h esAlg) Sign(payload []byte) ([]byte, error) {
signed, err := h.sign(payload)
func (es esAlg) Sign(payload []byte) ([]byte, error) {
signed, err := es.sign(payload)
if err != nil {
return nil, err
}

r, s, err := ecdsa.Sign(rand.Reader, h.privateKey, signed)
r, s, err := ecdsa.Sign(rand.Reader, es.privateKey, signed)
if err != nil {
return nil, err
}

keyBytes := h.SignSize() / 2
keyBytes := es.SignSize() / 2

rBytes, sBytes := r.Bytes(), s.Bytes()
signature := make([]byte, keyBytes*2)
Expand All @@ -93,27 +93,27 @@ func (h esAlg) Sign(payload []byte) ([]byte, error) {
return signature, nil
}

func (h esAlg) Verify(payload, signature []byte) error {
if len(signature) != 2*h.keySize {
func (es esAlg) Verify(payload, signature []byte) error {
if len(signature) != 2*es.keySize {
return ErrInvalidSignature
}

signed, err := h.sign(payload)
signed, err := es.sign(payload)
if err != nil {
return err
}

r := big.NewInt(0).SetBytes(signature[:h.keySize])
s := big.NewInt(0).SetBytes(signature[h.keySize:])
r := big.NewInt(0).SetBytes(signature[:es.keySize])
s := big.NewInt(0).SetBytes(signature[es.keySize:])

if !ecdsa.Verify(h.publickey, signed, r, s) {
if !ecdsa.Verify(es.publickey, signed, r, s) {
return ErrInvalidSignature
}
return nil
}

func (h esAlg) sign(payload []byte) ([]byte, error) {
hasher := h.hash.New()
func (es esAlg) sign(payload []byte) ([]byte, error) {
hasher := es.hash.New()

_, err := hasher.Write(payload)
if err != nil {
Expand Down
22 changes: 11 additions & 11 deletions algo_hs.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,20 @@ type hsAlg struct {
hashPool *sync.Pool
}

func (h hsAlg) Algorithm() Algorithm {
return h.alg
func (hs hsAlg) Algorithm() Algorithm {
return hs.alg
}

func (h hsAlg) SignSize() int {
return h.hash.Size()
func (hs hsAlg) SignSize() int {
return hs.hash.Size()
}

func (h hsAlg) Sign(payload []byte) ([]byte, error) {
return h.sign(payload)
func (hs hsAlg) Sign(payload []byte) ([]byte, error) {
return hs.sign(payload)
}

func (h hsAlg) Verify(payload, signature []byte) error {
signed, err := h.sign(payload)
func (hs hsAlg) Verify(payload, signature []byte) error {
signed, err := hs.sign(payload)
if err != nil {
return err
}
Expand All @@ -88,11 +88,11 @@ func (h hsAlg) Verify(payload, signature []byte) error {
return nil
}

func (h hsAlg) sign(payload []byte) ([]byte, error) {
hasher := h.hashPool.Get().(hash.Hash)
func (hs hsAlg) sign(payload []byte) ([]byte, error) {
hasher := hs.hashPool.Get().(hash.Hash)
defer func() {
hasher.Reset()
h.hashPool.Put(hasher)
hs.hashPool.Put(hasher)
}()

_, err := hasher.Write(payload)
Expand Down
24 changes: 12 additions & 12 deletions algo_ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,42 +78,42 @@ type psAlg struct {
opts *rsa.PSSOptions
}

func (h psAlg) SignSize() int {
return h.privateKey.Size()
func (ps psAlg) SignSize() int {
return ps.privateKey.Size()
}

func (h psAlg) Algorithm() Algorithm {
return h.alg
func (ps psAlg) Algorithm() Algorithm {
return ps.alg
}

func (h psAlg) Sign(payload []byte) ([]byte, error) {
signed, err := h.sign(payload)
func (ps psAlg) Sign(payload []byte) ([]byte, error) {
signed, err := ps.sign(payload)
if err != nil {
return nil, err
}

signature, err := rsa.SignPSS(rand.Reader, h.privateKey, h.hash, signed, h.opts)
signature, err := rsa.SignPSS(rand.Reader, ps.privateKey, ps.hash, signed, ps.opts)
if err != nil {
return nil, err
}
return signature, nil
}

func (h psAlg) Verify(payload, signature []byte) error {
signed, err := h.sign(payload)
func (ps psAlg) Verify(payload, signature []byte) error {
signed, err := ps.sign(payload)
if err != nil {
return err
}

err = rsa.VerifyPSS(h.publicKey, h.hash, signed, signature, h.opts)
err = rsa.VerifyPSS(ps.publicKey, ps.hash, signed, signature, ps.opts)
if err != nil {
return ErrInvalidSignature
}
return nil
}

func (h psAlg) sign(payload []byte) ([]byte, error) {
hasher := h.hash.New()
func (ps psAlg) sign(payload []byte) ([]byte, error) {
hasher := ps.hash.New()

_, err := hasher.Write(payload)
if err != nil {
Expand Down
24 changes: 12 additions & 12 deletions algo_rs.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,42 +58,42 @@ type rsAlg struct {
privateKey *rsa.PrivateKey
}

func (h rsAlg) Algorithm() Algorithm {
return h.alg
func (rs rsAlg) Algorithm() Algorithm {
return rs.alg
}

func (h rsAlg) SignSize() int {
return h.privateKey.Size()
func (rs rsAlg) SignSize() int {
return rs.privateKey.Size()
}

func (h rsAlg) Sign(payload []byte) ([]byte, error) {
signed, err := h.sign(payload)
func (rs rsAlg) Sign(payload []byte) ([]byte, error) {
signed, err := rs.sign(payload)
if err != nil {
return nil, err
}

signature, err := rsa.SignPKCS1v15(rand.Reader, h.privateKey, h.hash, signed)
signature, err := rsa.SignPKCS1v15(rand.Reader, rs.privateKey, rs.hash, signed)
if err != nil {
return nil, err
}
return signature, nil
}

func (h rsAlg) Verify(payload, signature []byte) error {
signed, err := h.sign(payload)
func (rs rsAlg) Verify(payload, signature []byte) error {
signed, err := rs.sign(payload)
if err != nil {
return err
}

err = rsa.VerifyPKCS1v15(h.publickey, h.hash, signed, signature)
err = rsa.VerifyPKCS1v15(rs.publickey, rs.hash, signed, signature)
if err != nil {
return ErrInvalidSignature
}
return nil
}

func (h rsAlg) sign(payload []byte) ([]byte, error) {
hasher := h.hash.New()
func (rs rsAlg) sign(payload []byte) ([]byte, error) {
hasher := rs.hash.New()

_, err := hasher.Write(payload)
if err != nil {
Expand Down

0 comments on commit d3fef3d

Please sign in to comment.