本文整理匯總了Golang中github.com/kisom/cryptutils/common/store.KeyStore類的典型用法代碼示例。如果您正苦於以下問題:Golang KeyStore類的具體用法?Golang KeyStore怎麽用?Golang KeyStore使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了KeyStore類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: importVerified
func importVerified(ks *store.KeyStore, cfg *config) error {
keyData, err := util.ReadFile(cfg.Args[0])
if err != nil {
return err
}
if cfg.Label == "self" {
cfg.Label, err = util.ReadLine("Label: ")
if err != nil {
return err
}
}
if !ks.ImportVerifiedKey(cfg.Label, keyData) {
return errors.New("verified import failed")
}
vkey, err := store.ParseVerifiedKey(keyData)
if err != nil {
return err
}
label, ok := ks.FindPublic(vkey.Signer)
if !ok {
return errors.New("invalid signer on key")
}
fmt.Printf("Imported public key signed by '%s'.\n", label)
return nil
}
示例2: keyList
func keyList(ks *store.KeyStore, cfg *config) error {
updated := time.Unix(ks.Timestamp, 0).Format(timeFormat)
fmt.Println("Key store was last updated", updated)
fmt.Printf("%d keys stored\n", len(ks.Keys))
fmt.Println("Owner public key:")
h := sha256.New()
h.Write(ks.PublicKey)
fmt.Printf("\tFingerprint: %x\n", h.Sum(nil))
if len(ks.Keys) > 0 {
fmt.Println("Key store:")
for k, v := range ks.Keys {
fmt.Printf("\t%s\n", k)
ut := time.Unix(v.Timestamp, 0)
st := time.Unix(v.SignatureTime, 0)
signer, ok := ks.FindPublic(v.KeySigner)
if !ok {
signer = "<unknown>"
}
h = sha256.New()
h.Write(v.Keys)
fmt.Printf("\t\tLast update: %s\n", ut.Format(timeFormat))
fmt.Printf("\t\t Signed at: %s\n", st.Format(timeFormat))
fmt.Printf("\t\t Signed by: %s\n", signer)
fmt.Printf("\t\tFingerprint: %x\n", h.Sum(nil))
}
}
return nil
}
示例3: cryptSignFile
func cryptSignFile(ks *store.KeyStore, cfg *config) error {
message, err := util.ReadFile(cfg.Args[0])
if err != nil {
util.Errorf("%v", err)
return err
}
out, ok := ks.EncryptAndSignTo(cfg.Label, message)
if !ok {
util.Errorf("Failed to encrypt message.")
return errors.New("signcryption failed")
}
if cfg.Armour {
block := pem.Block{
Type: public.SignedAndEncryptedType,
Bytes: out,
}
out = pem.EncodeToMemory(&block)
}
err = util.WriteFile(out, cfg.Args[1])
if err != nil {
util.Errorf("%v", err)
return nil
}
return nil
}
示例4: encryptFile
func encryptFile(ks *store.KeyStore, cfg *config) error {
message, err := util.ReadFile(cfg.Args[0])
if err != nil {
return err
}
out, ok := ks.EncryptTo(cfg.Label, message)
if !ok {
return errors.New("encryption failed")
}
if cfg.Armour {
block := pem.Block{
Type: public.EncryptedType,
Bytes: out,
}
out = pem.EncodeToMemory(&block)
}
err = util.WriteFile(out, cfg.Args[1])
if err != nil {
return err
}
return nil
}
示例5: decryptSignedFile
func decryptSignedFile(ks *store.KeyStore, cfg *config) error {
message, err := util.ReadFile(cfg.Args[0])
if err != nil {
return err
}
if len(message) > 10 {
if bytes.Equal(message[:10], []byte("-----BEGIN")) {
p, _ := pem.Decode(message)
if p == nil {
return errors.New("failed to decode PEM file")
}
switch p.Type {
case public.EncryptedType:
message = p.Bytes
default:
return errors.New("invalid message")
}
}
}
out, ok := ks.DecryptAndVerify(cfg.Label, message)
if !ok {
return errors.New("decrypt failed")
}
err = util.WriteFile(out, cfg.Args[1])
if err != nil {
return err
}
return nil
}
示例6: verifyFile
func verifyFile(ks *store.KeyStore, cfg *config) error {
message, err := util.ReadFile(cfg.Args[0])
if err != nil {
return err
}
sig, err := util.ReadFile(cfg.Args[1])
if err != nil {
return err
}
if len(sig) > 10 {
if bytes.Equal(sig[:10], []byte("-----BEGIN")) {
p, _ := pem.Decode(sig)
if p == nil {
return errors.New("failed to decode PEM file")
}
switch p.Type {
case public.SignatureType:
sig = p.Bytes
default:
return errors.New("invalid message")
}
}
}
if ks.Verify(cfg.Label, message, sig) {
fmt.Println("Signature: OK")
} else {
fmt.Println("Signature: INVALID")
}
return nil
}
示例7: keystoreDispatch
func keystoreDispatch(ks *store.KeyStore, keystoreFile string) {
for {
t := time.After(10 * time.Minute)
select {
case cmd, ok := <-dispatch:
if !ok {
return
}
log.Println("received command for", cmd.op)
res := process(ks, cmd)
cmd.cb <- res
case <-t:
log.Printf("dumping keystore")
t = time.After(10 * time.Minute)
out, err := ks.Dump()
if err != nil {
log.Printf("WARNING: failed to dump keystore: %v", err)
break
}
err = ioutil.WriteFile(keystoreFile, out, 0644)
if err != nil {
log.Printf("WARNING: failed to write keystore: %v", err)
}
}
}
}
示例8: exportVerified
func exportVerified(ks *store.KeyStore, cfg *config) error {
out, ok := ks.ExportVerified(cfg.Label)
if !ok {
return errors.New("failed to export verified public key")
}
return util.WriteFile(out, cfg.Args[0])
}
示例9: importUntrusted
func importUntrusted(ks *store.KeyStore, cfg *config) error {
fmt.Println("*****************************************")
fmt.Println("*** WARNING: IMPORTING UNTRUSTED KEYS ***")
fmt.Println("*****************************************")
keyData, err := util.ReadFile(cfg.Args[0])
if err != nil {
return err
}
vkey, err := store.ParseVerifiedKey(keyData)
if err != nil {
return err
}
if vkey.IsSelfSigned() {
fmt.Println("Key is self-signed.")
} else {
fmt.Println("Unrecognised signature.")
}
h := sha256.New()
h.Write(vkey.Public)
fmt.Printf("Fingerprint: %x\n", h.Sum(nil))
for {
line, err := util.ReadLine("\nAre you sure you want to import this key? (yes or no) ")
if err != nil {
return nil
}
if line == "yes" {
fmt.Println("As you wish.")
break
} else if line == "no" {
return errors.New("canceled by user")
} else {
fmt.Println("Please enter either 'yes' or 'no'.")
}
}
if cfg.Label == "self" {
cfg.Label, err = util.ReadLine("Label: ")
if err != nil {
return err
}
}
if !ks.AddKey(cfg.Label, vkey.Public, nil) {
return errors.New("failed to add new key")
}
return nil
}
示例10: removeKey
func removeKey(ks *store.KeyStore, cfg *config) error {
cfg.Label = cfg.Args[0]
if cfg.Label == "self" {
return errors.New("cannot remove own key")
} else if !ks.Has(cfg.Label) {
return fmt.Errorf("no key was found under label %s", cfg.Label)
}
fmt.Println("Removing key ", cfg.Label)
delete(ks.Keys, cfg.Label)
return nil
}
示例11: decryptFile
func decryptFile(ks *store.KeyStore, cfg *config) error {
message, err := util.ReadFile(cfg.Args[0])
if err != nil {
return err
}
var needVerify bool
if len(message) > 10 {
if bytes.Equal(message[:10], []byte("-----BEGIN")) {
p, _ := pem.Decode(message)
if p == nil {
return errors.New("failed to decode PEM file")
}
switch p.Type {
case public.EncryptedType:
message = p.Bytes
case public.SignedAndEncryptedType:
needVerify = true
message = p.Bytes
default:
return errors.New("invalid message")
}
}
}
var out []byte
var ok bool
if !needVerify {
out, ok = ks.Decrypt(message)
if !ok {
return errors.New("decrypt failed")
}
} else {
out, ok = ks.DecryptAndVerify(cfg.Label, message)
if !ok {
return errors.New("decrypt and verify failed")
}
fmt.Println("Valid signature.")
}
err = util.WriteFile(out, cfg.Args[1])
if err != nil {
return err
}
return nil
}
示例12: checkUpload
func checkUpload(ks *store.KeyStore, cmd command) response {
label := cmd.data["label"]
pub := []byte(cmd.data["public"])
var resp response
if label == "self" || label == "" {
resp.err = errors.New("invalid label")
return resp
}
ok := ks.ImportVerifiedKey(label, pub)
if !ok {
resp.err = errors.New("failed to add public key")
} else {
resp.out = []byte("public key added successfully")
}
return resp
}
示例13: signFile
func signFile(ks *store.KeyStore, cfg *config) error {
message, err := util.ReadFile(cfg.Args[0])
if err != nil {
return err
}
sig, ok := ks.Sign(message)
if !ok {
return errors.New("signing failed")
}
if cfg.Armour {
block := pem.Block{
Type: public.SignatureType,
Bytes: sig,
}
sig = pem.EncodeToMemory(&block)
}
return util.WriteFile(sig, cfg.Args[1])
}
示例14: checkStore
func checkStore(ks *store.KeyStore, cfg *config) error {
if ks.Locked() {
return errors.New("keystore is locked")
}
if !ks.Valid(false) {
return errors.New("keystore is invalid")
}
if !ks.KeyAudit() {
return errors.New("audit failed")
}
fmt.Println("Keystore is valid.")
return nil
}
示例15: unlockStore
func unlockStore(ks *store.KeyStore) bool {
if !ks.Locked() {
return true
}
passphrase, err := util.PassPrompt("keystore passphrase> ")
if err != nil {
util.Errorf("%v", err)
return false
}
defer util.Zero(passphrase)
if !ks.Locked() && ks.PrivateKey == nil {
if !ks.LockWith(passphrase) {
util.Errorf("Failed to set initial passphrase.")
return false
}
}
if !ks.Unlock(passphrase) {
util.Errorf("Unlock failed (bad passphrase?)")
return false
}
return true
}