passforios-gopenpgp/crypto/time.go
wussler e65ed17b41
Refactor api (#6)
* Refactor library, remove duplicates

* Rebuild structure to use Messages and Signature models

* Use PGPSplitMessage

* Remove signature model

* Various fixes

* Add helpers with tests

* Fixes, add some docs, add tests

* Add attachment helpers

* Add helpers Symmetric encryption

* Edit docs + examples

* Rename kr to keyRing

* Various fixes for documentation

* Edit JSON handling functions, add decrypt keyring via token

* Add proposal changes doc

* Fix CI

* Drop *Message functions, join CleartextMessage and BinaryMessage

* Change canonicalization and trimming only to text signatures

* Add cleartextsignature, detach signature from message model, move helpers

* Documentation, remove optional parameters

* Move verification to separate model

* Don't return message in VerifyDetached

* Update table of contents in readme

* Appease golint

* Run go fmt

* Rename Encrypt/DecryptMessageWithPassword to ..WithToken

These functions shouldn't be used with user-provided passwords,
as they don't do any key-stretching.

* Change key generation usernames
2019-06-03 17:00:01 +02:00

48 lines
1.1 KiB
Go

package crypto
import (
"time"
)
var pgp = GopenPGP{}
// GetGopenPGP return global GopenPGP
func GetGopenPGP() *GopenPGP {
return &pgp
}
// UpdateTime updates cached time
func (pgp *GopenPGP) UpdateTime(newTime int64) {
pgp.latestServerTime = newTime
pgp.latestClientTime = time.Now()
}
// GetUnixTime gets latest cached time
func (pgp *GopenPGP) GetUnixTime() int64 {
return pgp.getNow().Unix()
}
// GetTime gets latest cached time
func (pgp *GopenPGP) GetTime() time.Time {
return pgp.getNow()
}
// ----- INTERNAL FUNCTIONS -----
// getNow returns current time
func (pgp *GopenPGP) getNow() time.Time {
if pgp.latestServerTime > 0 && !pgp.latestClientTime.IsZero() {
// Until is monotonic, it uses a monotonic clock in this case instead of the wall clock
extrapolate := int64(time.Until(pgp.latestClientTime).Seconds())
return time.Unix(pgp.latestServerTime+extrapolate, 0)
}
return time.Now()
}
// getTimeGenerator Returns a time generator function
func (pgp *GopenPGP) getTimeGenerator() func() time.Time {
return func() time.Time {
return pgp.getNow()
}
}