Add key generation offset (#104)
* Add key generation offset * Bump version to 2.1.2
This commit is contained in:
parent
385e6d21d2
commit
75f27fd1df
6 changed files with 29 additions and 4 deletions
|
|
@ -8,6 +8,7 @@ import "time"
|
|||
type GopenPGP struct {
|
||||
latestServerTime int64
|
||||
latestClientTime time.Time
|
||||
generationOffset int64
|
||||
}
|
||||
|
||||
var pgp = GopenPGP{}
|
||||
|
|
|
|||
|
|
@ -435,7 +435,7 @@ func generateKey(
|
|||
cfg := &packet.Config{
|
||||
Algorithm: packet.PubKeyAlgoRSA,
|
||||
RSABits: bits,
|
||||
Time: getTimeGenerator(),
|
||||
Time: getKeyGenerationTimeGenerator(),
|
||||
DefaultHash: crypto.SHA256,
|
||||
DefaultCipher: packet.CipherAES256,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,6 +13,11 @@ func UpdateTime(newTime int64) {
|
|||
}
|
||||
}
|
||||
|
||||
// SetKeyGenerationOffset updates the offset when generating keys.
|
||||
func SetKeyGenerationOffset(offset int64) {
|
||||
pgp.generationOffset = offset
|
||||
}
|
||||
|
||||
// GetUnixTime gets latest cached time.
|
||||
func GetUnixTime() int64 {
|
||||
return getNow().Unix()
|
||||
|
|
@ -49,3 +54,19 @@ func getDiff() (int64, error) {
|
|||
func getTimeGenerator() func() time.Time {
|
||||
return getNow
|
||||
}
|
||||
|
||||
// getNowKeyGenerationOffset returns the current time with the key generation offset.
|
||||
func getNowKeyGenerationOffset() time.Time {
|
||||
extrapolate, err := getDiff()
|
||||
|
||||
if err != nil {
|
||||
return time.Unix(time.Now().Unix()+pgp.generationOffset, 0)
|
||||
}
|
||||
|
||||
return time.Unix(pgp.latestServerTime+extrapolate+pgp.generationOffset, 0)
|
||||
}
|
||||
|
||||
// getKeyGenerationTimeGenerator Returns a time generator function with the key generation offset.
|
||||
func getKeyGenerationTimeGenerator() func() time.Time {
|
||||
return getNowKeyGenerationOffset
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue