* Naming
* If this is not some OpenPGP standard I follow rule that `DES` should be
upper case as it is abreviation and `Triple` should be camel-case as it
is normal word hence `TripleDES`
* rename `errors2` -> `errorsPGP`
* long lines
* https://github.com/golang/go/wiki/CodeReviewComments#line-length
* I bit improved long lines based on my folding
* reuse type in definition if possible i.e. `a string, b string, c string` -> `a,b,c string`
* `if long_statetent(); err!=nil {` -> `long_statement;↵ if err!=nil {`
* spaces around operators (e.g. `a + b` -> `a+b`)
* removing empty lines on start and end of scope
* comments
* on all exported functions
* start with function name
* import:
* order in alphabet
* separate native, golang.org/x/ and our libs
44 lines
966 B
Go
44 lines
966 B
Go
package crypto
|
|
|
|
import (
|
|
"time"
|
|
)
|
|
|
|
var pmCrypto = PmCrypto{}
|
|
|
|
// GetPmCrypto return global PmCrypto
|
|
func GetPmCrypto() *PmCrypto {
|
|
return &pmCrypto
|
|
}
|
|
|
|
// UpdateTime updates cached time
|
|
func (pm *PmCrypto) UpdateTime(newTime int64) {
|
|
pm.latestServerTime = newTime
|
|
pm.latestClientTime = time.Now()
|
|
}
|
|
|
|
// GetTimeUnix gets latest cached time
|
|
func (pm *PmCrypto) GetTimeUnix() int64 {
|
|
return pm.getNow().Unix()
|
|
}
|
|
|
|
// GetTime gets latest cached time
|
|
func (pm *PmCrypto) GetTime() time.Time {
|
|
return pm.getNow()
|
|
}
|
|
|
|
func (pm *PmCrypto) getNow() time.Time {
|
|
if pm.latestServerTime > 0 && !pm.latestClientTime.IsZero() {
|
|
// Until is monotonic, it uses a monotonic clock in this case instead of the wall clock
|
|
extrapolate := int64(time.Until(pm.latestClientTime).Seconds())
|
|
return time.Unix(pm.latestServerTime+extrapolate, 0)
|
|
}
|
|
|
|
return time.Now()
|
|
}
|
|
|
|
func (pm *PmCrypto) getTimeGenerator() func() time.Time {
|
|
return func() time.Time {
|
|
return pm.getNow()
|
|
}
|
|
}
|