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

Truncate config.Now to second precision #168

Merged
merged 2 commits into from
May 18, 2023
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
7 changes: 5 additions & 2 deletions openpgp/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func TestExpiringPrimaryUIDKey(t *testing.T) {
}
}

func TestReturnFirstUnexpiredSigningSubkey(t *testing.T) {
func TestReturnNewestUnexpiredSigningSubkey(t *testing.T) {
// Make a master key.
entity, err := NewEntity("Golang Gopher", "Test Key", "[email protected]", nil)
if err != nil {
Expand All @@ -140,6 +140,9 @@ func TestReturnFirstUnexpiredSigningSubkey(t *testing.T) {

// Second signing subkey expires in a day.
err = entity.AddSigningSubkey(&packet.Config{
Time: func() time.Time {
return time.Now().Add(1 * time.Second)
},
KeyLifetimeSecs: 24 * 60 * 60,
})
if err != nil {
Expand All @@ -149,7 +152,7 @@ func TestReturnFirstUnexpiredSigningSubkey(t *testing.T) {
subkey2 := entity.Subkeys[2]

// Before second signing subkey has expired, it should be returned.
time1 := time.Now()
time1 := time.Now().Add(2 * time.Second)
expected := subkey2.PublicKey.KeyIdShortString()
subkey, found := entity.SigningKey(time1)
if !found {
Expand Down
8 changes: 4 additions & 4 deletions openpgp/packet/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type Config struct {
// and password-encrypted data.
// If nil, the default configuration is used
S2KConfig *s2k.Config
// Iteration count for Iterated S2K (String to Key).
// Iteration count for Iterated S2K (String to Key).
// Only used if sk2.Mode is nil.
// This value is duplicated here from s2k.Config for backwards compatibility.
// It determines the strength of the passphrase stretching when
Expand Down Expand Up @@ -135,9 +135,9 @@ func (c *Config) Cipher() CipherFunction {

func (c *Config) Now() time.Time {
if c == nil || c.Time == nil {
return time.Now()
return time.Now().Truncate(time.Second)
}
return c.Time()
return c.Time().Truncate(time.Second)
}

// KeyLifetime returns the validity period of the key.
Expand Down Expand Up @@ -198,7 +198,7 @@ func (c *Config) S2K() *s2k.Config {
}
// for backwards compatibility
if c != nil && c.S2KCount > 0 && c.S2KConfig == nil {
return &s2k.Config {
return &s2k.Config{
S2KCount: c.S2KCount,
}
}
Expand Down