示例#1
0
func (s *CryptoSuite) TestNewPasswordKeyFail(c *C) {
	key, salt, err := crypto.NewPasswordKey("", 1)
	c.Assert(key, IsNil)
	c.Assert(salt, IsNil)
	c.Assert(err, NotNil)
	key, salt, err = crypto.NewPasswordKey("pass", 0)
	c.Assert(key, IsNil)
	c.Assert(salt, IsNil)
	c.Assert(err, NotNil)
}
示例#2
0
func (s *CryptoSuite) TestNewLoadPasswordKey(c *C) {
	password := "******"
	stretch := 1
	newKey, salt, _ := crypto.NewPasswordKey(password, stretch)
	loadKey, _ := crypto.LoadPasswordKey(password, salt, stretch)
	c.Assert(newKey, DeepEquals, loadKey)
}
示例#3
0
文件: store.go 项目: schmich/ward
func createCipher(db *sql.DB, password string, passwordStretch int) (*crypto.Cipher, *crypto.Cipher, error) {
	const version = 1

	tx, err := db.Begin()
	if err != nil {
		return nil, nil, err
	}

	defer func() {
		if err != nil {
			tx.Rollback()
		} else {
			tx.Commit()
		}
	}()

	passwordKey, passwordSalt, err := crypto.NewPasswordKey(password, passwordStretch)
	if err != nil {
		return nil, nil, err
	}

	passwordCipher, err := crypto.NewCipher(passwordKey)
	if err != nil {
		return nil, nil, err
	}

	key := crypto.NewKey()
	keyCipher, err := crypto.NewCipher(key)
	if err != nil {
		return nil, nil, err
	}

	insert, err := tx.Prepare(`
    INSERT INTO settings (password_salt, password_stretch, password_nonce, encrypted_key, key_nonce, version)
    VALUES (?, ?, ?, ?, ?, ?)
  `)

	if err != nil {
		return nil, nil, err
	}

	defer insert.Close()

	encryptedKey := passwordCipher.Encrypt(key)

	insert.Exec(
		passwordSalt,
		passwordStretch,
		passwordCipher.GetNonce(),
		encryptedKey,
		keyCipher.GetNonce(),
		version)

	return passwordCipher, keyCipher, nil
}
示例#4
0
文件: store.go 项目: schmich/ward
func (store *Store) UpdateMasterPassword(password string, passwordStretch int) error {
	return store.update(func(tx *sql.Tx) error {
		query := "SELECT encrypted_key FROM settings"

		rows, err := tx.Query(query)
		if err != nil {
			return err
		}

		defer rows.Close()
		rows.Next()

		var encryptedKey []byte
		rows.Scan(&encryptedKey)

		key := store.passwordCipher.Decrypt(encryptedKey)

		passwordKey, passwordSalt, err := crypto.NewPasswordKey(password, passwordStretch)
		if err != nil {
			return err
		}

		passwordCipher, err := crypto.NewCipher(passwordKey)
		if err != nil {
			return err
		}

		update, err := tx.Prepare(`
      UPDATE settings
      SET password_salt=?, password_stretch=?, password_nonce=?, encrypted_key=?
    `)

		if err != nil {
			return err
		}

		defer update.Close()

		update.Exec(
			passwordSalt,
			passwordStretch,
			passwordCipher.GetNonce(),
			passwordCipher.Encrypt(key))

		store.passwordCipher = passwordCipher

		return nil
	})
}