Skip to content

Commit 75f27fd

Browse files
authored
Add key generation offset (#104)
* Add key generation offset * Bump version to 2.1.2
1 parent 385e6d2 commit 75f27fd

File tree

6 files changed

+29
-4
lines changed

6 files changed

+29
-4
lines changed

CHANGELOG.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7-
## Unreleased
7+
## [2.1.2] 2020-12-01
8+
### Added
9+
- `SetKeyGenerationOffset` to add an offset in key generation time and prevent not-yet-valid keys.
10+
811
### Changed
912
- Improved canonicalization performance
1013

constants/armor.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package constants
33

44
// Constants for armored data.
55
const (
6-
ArmorHeaderVersion = "GopenPGP 2.1.1"
6+
ArmorHeaderVersion = "GopenPGP 2.1.2"
77
ArmorHeaderComment = "https://gopenpgp.org"
88
PGPMessageHeader = "PGP MESSAGE"
99
PGPSignatureHeader = "PGP SIGNATURE"

constants/version.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
package constants
22

3-
const Version = "ddacebe0"
3+
const Version = "2.1.2"

crypto/gopenpgp.go

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import "time"
88
type GopenPGP struct {
99
latestServerTime int64
1010
latestClientTime time.Time
11+
generationOffset int64
1112
}
1213

1314
var pgp = GopenPGP{}

crypto/key.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ func generateKey(
435435
cfg := &packet.Config{
436436
Algorithm: packet.PubKeyAlgoRSA,
437437
RSABits: bits,
438-
Time: getTimeGenerator(),
438+
Time: getKeyGenerationTimeGenerator(),
439439
DefaultHash: crypto.SHA256,
440440
DefaultCipher: packet.CipherAES256,
441441
}

crypto/time.go

+21
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ func UpdateTime(newTime int64) {
1313
}
1414
}
1515

16+
// SetKeyGenerationOffset updates the offset when generating keys.
17+
func SetKeyGenerationOffset(offset int64) {
18+
pgp.generationOffset = offset
19+
}
20+
1621
// GetUnixTime gets latest cached time.
1722
func GetUnixTime() int64 {
1823
return getNow().Unix()
@@ -49,3 +54,19 @@ func getDiff() (int64, error) {
4954
func getTimeGenerator() func() time.Time {
5055
return getNow
5156
}
57+
58+
// getNowKeyGenerationOffset returns the current time with the key generation offset.
59+
func getNowKeyGenerationOffset() time.Time {
60+
extrapolate, err := getDiff()
61+
62+
if err != nil {
63+
return time.Unix(time.Now().Unix()+pgp.generationOffset, 0)
64+
}
65+
66+
return time.Unix(pgp.latestServerTime+extrapolate+pgp.generationOffset, 0)
67+
}
68+
69+
// getKeyGenerationTimeGenerator Returns a time generator function with the key generation offset.
70+
func getKeyGenerationTimeGenerator() func() time.Time {
71+
return getNowKeyGenerationOffset
72+
}

0 commit comments

Comments
 (0)