티스토리 뷰
bcrypt 설치
go get -u -v golang.org/x/crypto/bcrypt
예제 코드
package main
import (
"golang.org/x/crypto/bcrypt"
)
func main() {
pwd := "abcdefg1234"
hashedPwd, err := GeneratePassword(pwd)
if err == nil {
equalPwd := EqualPassword(hashedPwd, pwd)
fmt.Println("hashed password : ", hashedPwd)
fmt.Println("equal password : ", equalPwd)
}
}
func GeneratePassword(password string) (string, error) {
pass := []byte(password)
hashed, err := bcrypt.GenerateFromPassword(pass, bcrypt.DefaultCost)
return string(hashed), err
}
func EqualPassword(hashedPassword string, password string) bool {
return bcrypt.CompareHashAndPassword(
[]byte(hashedPassword),
[]byte(password)) == nil
}
결과
hashed password : $2a$10$zwLky147As5Ch4rwEG7Ih./v68OTq0vWyXyBPcnmvMYKdL82eAXr6 equal password : true
'프로그래밍 > Go' 카테고리의 다른 글
| [Windows] GO로 개발한 어플 실행 시 Console 윈도우 숨기기 (0) | 2018.08.21 |
|---|---|
| Golang HTTPS/TLS 예제 (0) | 2018.07.10 |
| [windows] cmd에서 ansi color 활성화 방법 (0) | 2018.07.03 |
| Go 웹 어플리케이션 자동 빌드 및 재시작 (0) | 2015.11.02 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
