Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use bytes.Clone instead of manual cloning #67

Merged
merged 2 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cng/aes.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package cng

import (
"bytes"
"crypto/cipher"
"errors"
"runtime"
Expand All @@ -28,8 +29,7 @@ func NewAESCipher(key []byte) (cipher.Block, error) {
if err != nil {
return nil, err
}
c := &aesCipher{kh: kh, key: make([]byte, len(key))}
copy(c.key, key)
c := &aesCipher{kh: kh, key: bytes.Clone(key)}
runtime.SetFinalizer(c, (*aesCipher).finalize)
return c, nil
}
Expand Down
7 changes: 3 additions & 4 deletions cng/des.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package cng

import (
"bytes"
"crypto/cipher"
"runtime"

Expand All @@ -27,8 +28,7 @@ func NewDESCipher(key []byte) (cipher.Block, error) {
if err != nil {
return nil, err
}
c := &desCipher{kh: kh, alg: bcrypt.DES_ALGORITHM, key: make([]byte, len(key))}
copy(c.key, key)
c := &desCipher{kh: kh, alg: bcrypt.DES_ALGORITHM, key: bytes.Clone(key)}
runtime.SetFinalizer(c, (*desCipher).finalize)
return c, nil
}
Expand All @@ -38,8 +38,7 @@ func NewTripleDESCipher(key []byte) (cipher.Block, error) {
if err != nil {
return nil, err
}
c := &desCipher{kh: kh, alg: bcrypt.DES3_ALGORITHM, key: make([]byte, len(key))}
copy(c.key, key)
c := &desCipher{kh: kh, alg: bcrypt.DES3_ALGORITHM, key: bytes.Clone(key)}
runtime.SetFinalizer(c, (*desCipher).finalize)
return c, nil
}
Expand Down
16 changes: 3 additions & 13 deletions cng/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package cng

import (
"bytes"
"crypto"
"hash"
"runtime"
Expand Down Expand Up @@ -194,12 +195,7 @@ func newHashX(id string, flag bcrypt.AlgorithmProviderFlags, key []byte) *hashX
if err != nil {
panic(err)
}
h := new(hashX)
h.alg = alg
if len(key) > 0 {
h.key = make([]byte, len(key))
copy(h.key, key)
}
h := &hashX{alg: alg, key: bytes.Clone(key)}
// Don't allocate hx.buf nor call bcrypt.CreateHash yet,
// which would be wasteful if the caller only wants to know
// the hash type. This is a common pattern in this package,
Expand Down Expand Up @@ -227,13 +223,7 @@ func (h *hashX) withCtx(fn func(ctx bcrypt.HASH_HANDLE) error) error {
}

func (h *hashX) Clone() (hash.Hash, error) {
h2 := &hashX{
alg: h.alg,
}
if h.key != nil {
h2.key = make([]byte, len(h.key))
copy(h2.key, h.key)
}
h2 := &hashX{alg: h.alg, key: bytes.Clone(h.key)}
err := h.withCtx(func(ctx bcrypt.HASH_HANDLE) error {
return bcrypt.DuplicateHash(ctx, &h2._ctx, nil, 0)
})
Expand Down
Loading