Пример #1
0
func TestEncryptDecrypt(t *testing.T) {
	e := vsafe.Entry{
		Title:    "title",
		Desc:     "desc",
		UName:    "foo",
		Password: "******",
		Special:  "baz"}
	key := &vsafe.Key{Id: 1, Value: kdf.Random(32)}
	encrypted := e
	if err := encrypted.Encrypt(key); err != nil {
		t.Fatalf("Got error encrypting: %v", err)
	}
	if encrypted.UName == e.UName || encrypted.Password == e.Password || encrypted.Special == e.Special {
		t.Error("Encrypted is the same as plain text")
	}
	if encrypted.Title != e.Title || encrypted.Desc != e.Desc {
		t.Error("Encrypted fields should be the same as plain text")
	}
	decrypted := encrypted
	if err := decrypted.Decrypt(key); err != nil {
		t.Fatalf("Got error encrypting: %v", err)
	}
	// Owner of entity changes to key Id
	e.Owner = key.Id
	if decrypted != e {
		t.Errorf("Expected %v, got %v", e, decrypted)
	}
	if err := decrypted.Decrypt(&vsafe.Key{Id: 2, Value: kdf.Random(32)}); err != vsafe.ErrKeyMismatch {
		t.Errorf("Expected ErrKeyMismatch, got %v", err)
	}
}
Пример #2
0
// EncryptB encrypts plain with key and returns a base64 encoded string.
func EncryptB(plain, key []byte) (string, error) {
	block, err := aes.NewCipher(key)
	if err != nil {
		return "", err
	}
	result := make([]byte, len(plain)+aes.BlockSize)
	iv := kdf.Random(aes.BlockSize)
	idx := copy(result, iv)
	mode := cipher.NewCBCEncrypter(block, iv)
	mode.CryptBlocks(result[idx:], []byte(plain))
	return base64.StdEncoding.EncodeToString(result), nil
}
Пример #3
0
// SetUserId sets the user ID in this session and generates a new xsrf secret
// for creating xsrf tokens.
func (s UserIdSession) SetUserId(id int64) {
	s.S.Values[kUserIdKey] = id
	s.setXsrfSecret(kdf.Random(64))
}
Пример #4
0
// Init initializes this user instance with a user name and password so that
// this user is a master user and has its own random key.
func (u *User) Init(name, password string) error {
	return u.InitWithKey(name, password, &Key{Id: 0, Value: kdf.Random(32)})
}
Пример #5
0
	"net/url"
	"testing"
)

var (
	kAnEntry = &vsafe.Entry{
		UName:    "uname",
		Password: "******",
		Special:  "special",
	}
	kOrigEntry = &vsafe.Entry{
		UName:    "somename",
		Password: "******",
		Special:  "xxx",
	}
	kKey                        = &vsafe.Key{Id: 7, Value: kdf.Random(32)}
	kTransaction db.Transaction = 0
)

func TestAddEntry(t *testing.T) {
	var store FakeStore
	entry := *kAnEntry
	var id int64
	var err error
	if id, err = vsafedb.AddEntry(&store, nil, kKey, &entry); err != nil {
		t.Fatalf("Error adding tostore: %v", err)
	}
	if id != 1 {
		t.Errorf("expected 1, got %d", id)
	}
	// entry should not change as side effect