未加星标

Securely Storing Password Data in Couchbase with Golang and BCrypt

字体大小 | |
[数据库(综合) 所属分类 数据库(综合) | 发布者 店小二04 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏

When writing web applications that store passwords for your users, it is never a good idea to store them as plain text in your database, whether that be NoSQL or RDBMS. Last year I wrote about using BCrypt with Node.js and Couchbase as well as using BCrypt with Java and Couchbase. What if we wanted to take this to Golang?

We’re going to see how to hash passwords in a Golang application using BCrypt to safely store them in Couchbase.

To set the record straight, we’re interested in password hashing, not password encryption because encryption implies that decryption is possible. Hashing is a one way process.

The great thing about Go is that there is a a BCrypt package built in. When using this package or doing anything related to BCrypt, you’re going to focus on two functions. The generation of a hash and the comparison or validation of a hash.

In Go, you could generate a new hash based on your password string like the following:

hash, err := bcrypt.GenerateFromPassword([]byte("my-password"), 10)

Using the GenerateFromPassword function, you pass in a string as well as a cost value. The higher the cost value, the slower the function because the cost value is how many times the hash is applied.

If you ever wanted to see if a plaintext password matches a hashed password, you could execute the following:

err = bcrypt.CompareHashAndPassword([]byte("hashed-password"), []byte("plaintext-password"))

If there is no error, then the passwords matched.

So how might you use this password hashing functionality with Couchbase?

Take the following for example:

type Account struct {
Type string `json:"type,omitempty"`
Email string `json:"email,omitempty"`
Password string `json:"password,omitempty"`
}
func main() {
cluster, _ := gocb.Connect("couchbase://localhost")
bucket, _ := cluster.OpenBucket("default", "")
email := "[email protected] " pass := "my-password" hash, _ := bcrypt.GenerateFromPassword([]byte(pass), 10) account := Account{ Type: "account", Email: email, Password: string(hash), } bucket.Insert(email, account, 0) account = Account{} bucket.Get(email, &account) err := bcrypt.CompareHashAndPassword([]byte(account.Password), []byte(pass)) if err != nil { fmt.Println(err.Error()) } }

The above is a very crude example, but it gets the point across on how you might store the BCrypt password and how to compare against it.

Conclusion

You just saw how to use BCrypt in a Golang application. It is critical that sensitive information like passwords be hashed before they end up in your database, whether that be NoSQL or something else.

For more information on using Couchbase with Golang, check out the Couchbase Developer Portal.

The post Securely Storing Password Data in Couchbase with Golang and BCrypt appeared first on The Couchbase Blog.

本文数据库(综合)相关术语:系统安全软件

主题: SQLJavaNode.js
分页:12
转载请注明
本文标题:Securely Storing Password Data in Couchbase with Golang and BCrypt
本站链接:http://www.codesec.net/view/561398.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 数据库(综合) | 评论(0) | 阅读(35)