Golang bcrypt 패스워드 해시

1 개요[ | ]

Golang bcrypt 패스워드 해시
Golang 라라벨 패스워드 해시
Golang OpenSearch 패스워드 해시
package main

import (
	"fmt"

	"golang.org/x/crypto/bcrypt"
)

func HashPassword(plain string) string {
	hashed, _ := bcrypt.GenerateFromPassword([]byte(plain), bcrypt.DefaultCost)
	return string(hashed)
}

func CheckPassword(plain string, hashed string) bool {
	return bcrypt.CompareHashAndPassword([]byte(hashed), []byte(plain)) == nil
}

func main() {
	// test HashPassword()
	fmt.Println("== test HashPassword() ==")
	hashed1 := HashPassword("lorem")
	hashed2 := HashPassword("무궁화 꽃이 피었습니다.")
	fmt.Println(CheckPassword("lorem", hashed1))         // true
	fmt.Println(CheckPassword("ipsum", hashed1))         // false
	fmt.Println(CheckPassword("무궁화 꽃이 피었습니다.", hashed2)) // true
	fmt.Println(CheckPassword("무궁화 꽃이 피었습니까?", hashed2)) // false

	// https://opensearch.org/docs/1.3/security-plugin/configuration/yaml#internal_usersyml
	fmt.Println("== test CheckPassword() for OpenSearch hash ==")
	fmt.Println(CheckPassword("admin", "$2a$12$VcCDgh2NDk07JGN0rjGbM.Ad41qVR/YFJcgHp0UGns5JDymv..TOG"))        // true
	fmt.Println(CheckPassword("kibanaserver", "$2a$12$4AcgAt3xwOWadA5s5blL6ev39OXDNhmOesEoo33eZtrq2N0YrU3H.")) // true
	fmt.Println(CheckPassword("logstash", "$2a$12$u1ShR4l4uBS3Uv59Pa2y5.1uQuZBrZtmNfqB3iM/.jL0XoV9sghS2"))     // true

	fmt.Println("== test CheckPassword() for Laravel hash ==")
	fmt.Println(CheckPassword("hello", "$2y$10$aJ1sYcwUUMCgaH2IQ3axVOUqsyrrQD6KrASSIbTZqvi3vzS.U8Q4S")) // true
	fmt.Println(CheckPassword("hello", "$2y$10$vq2qK5kHSbcGZmMAj9y/iuST83wBZwttj0/Xz/waV0nv1nQB23ywO")) // true
	fmt.Println(CheckPassword("world", "$2y$10$ESQP50Cv9Q.PD0BilQrcWufK02LdBm5gVFeRwJ0LZziRIoKMwxlsa")) // true

	fmt.Println("== test CheckPassword() for htpasswd hash ==")
	fmt.Println(CheckPassword("topsecret", "$2y$10$a45aTOdIKkMab9JAtZ24AuUMofrhf/xbuiu.UsI2AH2ZyBcIk57cC")) // true
	fmt.Println(CheckPassword("topsecret", "$2y$12$KTbZnVgxAIUmnu5W2bRGmuJ/in8A9sHLt2je2lxOriq8TJP0vMk1y")) // true
}

2 같이 보기[ | ]

3 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}