-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbcrpyt.go
39 lines (34 loc) · 867 Bytes
/
bcrpyt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package main
import (
"bufio"
"fmt"
"log"
"os"
"golang.org/x/crypto/bcrypt"
)
func hashPassword(password []byte) ([]byte, error) {
hashedPassword, err := bcrypt.GenerateFromPassword(password, bcrypt.DefaultCost)
if err != nil {
return nil, fmt.Errorf(" hashPassword was not able to work: %w", err)
}
return hashedPassword, nil
}
func comparePassword(password, hashedPassword []byte) error {
err := bcrypt.CompareHashAndPassword(hashedPassword, password)
if err != nil {
return fmt.Errorf("comparedPassword : invalid password %w", err)
}
return nil
}
func testcrpyt() {
password := []byte("varun123")
s := bufio.NewScanner(os.Stdin)
s.Scan()
userinput := s.Bytes()
hashedPassword, _ := hashPassword(password)
err := comparePassword(userinput, hashedPassword)
if err != nil {
log.Panic("Invalid password")
}
fmt.Println("Logged in!")
}