本文整理汇总了Golang中github.com/kisom/cryptutils/common/util.Errorf函数的典型用法代码示例。如果您正苦于以下问题:Golang Errorf函数的具体用法?Golang Errorf怎么用?Golang Errorf使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Errorf函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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
}
示例2: loadStore
func loadStore(path string, m secret.ScryptMode) *store.SecretStore {
passphrase, err := util.PassPrompt("Secrets passphrase> ")
if err != nil {
util.Errorf("Failed to read passphrase: %v", err)
return nil
}
var passwords *store.SecretStore
if ok, _ := util.Exists(path); ok {
defer util.Zero(passphrase)
fileData, err := util.ReadFile(path)
if err != nil {
util.Errorf("%v", err)
return nil
}
var ok bool
passwords, ok = store.UnmarshalSecretStore(fileData, passphrase, m)
if !ok {
return nil
}
return passwords
}
util.Errorf("could not find %s", path)
return nil
}
示例3: LoadKeyStore
// LoadKeyStore attempts to load a keystore from the given path. If
// the keystore doesn't exist, a new one is created with a
// freshly-generated keys if the orNew argument is true.
func LoadKeyStore(path string, orNew bool) (*KeyStore, bool) {
data, err := ioutil.ReadFile(path)
if err != nil && os.IsNotExist(err) && orNew {
var priv *public.PrivateKey
priv, err = public.GenerateKey()
if err != nil {
util.Errorf("Failed to generate key.")
return nil, false
}
return NewPrivateKeyStore(priv)
} else if err != nil {
util.Errorf("%v", err)
return nil, false
}
store := new(KeyStore)
err = json.Unmarshal(data, store)
if err != nil {
util.Errorf("%v", err)
return nil, false
}
if store.Keys == nil {
store.Keys = map[string]*PublicKeyRecord{}
}
if store.PrivateKey != nil {
store.locked = true
}
if !store.Valid(true) {
util.Errorf("invalid keystore %v", store)
return nil, false
}
return store, true
}
示例4: UnmarshalSecretStore
// UnmarshalSecretStore decrypts and parses the secret store contained
// in the input byte slice.
func UnmarshalSecretStore(in, passphrase []byte, m secret.ScryptMode) (*SecretStore, bool) {
if len(in) < saltSize {
return nil, false
}
salt := in[:saltSize]
enc := in[saltSize:]
key := secret.DeriveKeyStrength(passphrase, salt, m)
if key == nil {
return nil, false
}
defer util.Zero(key[:])
data, ok := secret.Decrypt(key, enc)
if !ok {
util.Errorf("decrypt fails")
return nil, false
}
defer util.Zero(data)
var store SecretStore
err := json.Unmarshal(data, &store)
if err != nil {
util.Errorf("encrypt fails")
return nil, false
}
store.passphrase = make([]byte, len(passphrase))
copy(store.passphrase, passphrase)
return &store, true
}
示例5: loadStore
func loadStore(path string) *store.KeyStore {
// If keystore is newly created, we'll want to write it to
// disk before leaving this function.
var flush bool
if exists, _ := util.Exists(path); !exists {
flush = true
}
passphrase, err := util.PassPrompt("keystore passphrase> ")
if err != nil {
util.Errorf("%v", err)
return nil
}
defer util.Zero(passphrase)
keystore, ok := store.LoadKeyStore(path, true)
if !ok {
fmt.Printf("error in LoadKeyStore")
return nil
}
if !keystore.Valid(false) {
fmt.Println("keystore not valid")
return nil
}
if !flush {
if !keystore.Unlock(passphrase) {
return nil
}
return keystore
}
if !keystore.LockWith(passphrase) {
util.Errorf("Failed to set initial passphrase.")
return nil
} else if !keystore.Unlock(passphrase) {
util.Errorf("Flushing keystore failed.")
return nil
}
out, err := keystore.Dump()
if err != nil {
log.Printf("WARNING: failed to dump keystore: %v", err)
return nil
}
err = ioutil.WriteFile(path, out, 0644)
if err != nil {
log.Printf("WARNING: failed to write keystore: %v", err)
}
return keystore
}
示例6: writeStore
func writeStore(ks *store.KeyStore, path string) bool {
storeData := store.DumpKeyStore(ks)
if storeData == nil {
util.Errorf("Failed to dump keystore.")
return false
}
err := ioutil.WriteFile(path, storeData, 0644)
if err != nil {
util.Errorf("%v", err)
return false
}
return true
}
示例7: addSecret
func addSecret(ps *store.SecretStore, cfg *config, m secret.ScryptMode) error {
if cfg.WithMeta {
return addMeta(ps, cfg, m)
}
label := cfg.Args[0]
var rec *store.SecretRecord
if ps.Has(label) {
if !cfg.Overwrite {
return errors.New("entry exists, not forcing overwrite")
}
util.Errorf("*** WARNING: overwriting password")
rec = ps.Store[label]
} else {
rec = &store.SecretRecord{Label: label}
}
password, err := readpass.PasswordPromptBytes("New password: ")
if err != nil {
return err
} else if len(password) == 0 {
return errors.New("no password entered")
}
rec.Secret = password
rec.Timestamp = time.Now().Unix()
ps.Store[label] = rec
return nil
}
示例8: removeMeta
func removeMeta(ps *store.SecretStore, cfg *config) error {
label := cfg.Args[0]
if !ps.Has(label) {
return errors.New("entry not found")
}
rec := ps.Store[label]
for {
var keys = make([]string, 0, len(rec.Metadata))
for k := range rec.Metadata {
keys = append(keys, k)
}
sort.Strings(keys)
fmt.Println("Keys:")
for i := range keys {
fmt.Printf("\t%s\n", keys[i])
}
key, err := util.ReadLine("Remove key: ")
if err != nil {
util.Errorf("Failed to read key: %v", err)
continue
} else if key == "" {
break
}
delete(rec.Metadata, key)
fmt.Println("Deleted key", key)
}
rec.Timestamp = time.Now().Unix()
ps.Store[label] = rec
return nil
}
示例9: initStore
func initStore(path string, m secret.ScryptMode) error {
passphrase, err := util.PassPrompt("Secrets passphrase> ")
if err != nil {
util.Errorf("Failed to read passphrase: %v", err)
return err
}
if len(passphrase) == 0 {
return fmt.Errorf("No passphrase provided.")
}
defer util.Zero(passphrase)
passwords := store.NewSecretStore(passphrase)
if passwords == nil {
return fmt.Errorf("failed to create store")
}
fmt.Println("creating store...")
fileData, ok := store.MarshalSecretStore(passwords, m)
if !ok {
return fmt.Errorf("failed to marshal store")
}
err = util.WriteFile(fileData, path)
if err != nil {
return err
}
passwords, ok = store.UnmarshalSecretStore(fileData, passphrase, m)
if !ok {
err = fmt.Errorf("failed to unmarshal store")
}
return err
}
示例10: migrateStore
// migrateStore decrypts a file using the legacy settings for scrypt,
// and re-encrypts with the current settings.
func migrateStore(path string) {
prompt := fmt.Sprintf("Password for %s: ", path)
pass, err := readpass.PasswordPromptBytes(prompt)
if err != nil {
util.Errorf("%v", err)
os.Exit(1)
}
blob, err := legacy.DecryptFile(path, pass)
if err != nil {
util.Errorf("%v", err)
os.Exit(1)
}
fmt.Printf("[+] Decrypted %s.\n", path)
err = secret.EncryptFile(path, pass, blob)
if err != nil {
util.Errorf("%v", err)
os.Exit(1)
}
fmt.Printf("[+] Migrated %s.\n", path)
}
示例11: KeyAudit
// KeyAudit verifies the signature chain on all keys in the
// keystore. This operation may be slow, and it is recommended that it
// be run at most once per hour. For large keystores, once per day
// might be more suitable.
func (s *KeyStore) KeyAudit() bool {
if !s.Valid(true) {
return false
}
for lbl := range s.Keys {
if !s.VerifyChain(lbl) {
util.Errorf("Key %s is not trusted.", lbl)
return false
}
}
return true
}
示例12: writeStore
func writeStore(ps *store.SecretStore, path string, m secret.ScryptMode) bool {
fileData, ok := store.MarshalSecretStore(ps, m)
if !ok {
return false
}
err := util.WriteFile(fileData, path)
if err != nil {
util.Errorf("Write failed: %v", err)
return false
}
return true
}
示例13: multi
func multi(ps *store.SecretStore, cfg *config, m secret.ScryptMode) error {
fmt.Println("Use an empty name to indicate that you are done.")
for {
name, err := util.ReadLine("Name: ")
if err != nil {
return err
} else if name == "" {
break
}
var rec *store.SecretRecord
if ps.Has(name) {
if !cfg.Overwrite {
util.Errorf("Entry exists, not forcing overwrite.")
continue
} else {
util.Errorf("*** WARNING: overwriting password")
}
rec = ps.Store[name]
} else {
rec = &store.SecretRecord{
Label: name,
}
}
password, err := util.PassPrompt("Password: ")
if err != nil {
return err
} else if len(password) == 0 {
util.Errorf("No password entered.")
continue
}
rec.Secret = password
rec.Timestamp = time.Now().Unix()
ps.Store[name] = rec
}
return nil
}
示例14: 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
}
示例15: writeMeta
func writeMeta(args []string) error {
if len(args) == 0 {
return errors.New("no label specified")
} else if len(args) > 1 {
return errors.New("only one label may be specified")
}
label := args[0]
r, ok := session.Store.Store[label]
if !ok {
return errors.New("no such record")
}
if r.Metadata == nil {
r.Metadata = map[string][]byte{}
}
fmt.Println("Enter metadata; use an empty line to indicate that you are done.")
for {
line, err := util.ReadLine("key = value: ")
if err != nil {
return err
} else if line == "" {
break
}
meta := strings.SplitN(line, "=", 2)
if len(meta) < 2 {
util.Errorf("Metadata should be in the form 'key=value'")
continue
}
key := strings.TrimSpace(meta[0])
val := strings.TrimSpace(meta[1])
if prev, ok := r.Metadata[key]; ok {
fmt.Printf("Note: replacing previous value of '%s'\n", string(prev))
}
r.Metadata[key] = []byte(val)
}
r.Timestamp = time.Now().Unix()
session.Store.Timestamp = r.Timestamp
session.Store.Store[label] = r
session.Dirty = true
return nil
}