Skip to content

Commit

Permalink
feat: encrypts the passwd
Browse files Browse the repository at this point in the history
Signed-off-by: daz-3ux <[email protected]>
  • Loading branch information
Daz-3ux committed Sep 23, 2023
1 parent 90d5764 commit 162a4ac
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 2 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/spf13/viper v1.16.0
go.uber.org/automaxprocs v1.5.3
go.uber.org/zap v1.25.0
golang.org/x/crypto v0.9.0
gorm.io/driver/mysql v1.5.1
gorm.io/gorm v1.25.4
)
Expand Down Expand Up @@ -53,7 +54,6 @@ require (
github.com/ugorji/go/codec v1.2.11 // indirect
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
Expand Down
5 changes: 5 additions & 0 deletions internal/dazBlog/controller/v1/user/create.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// Copyright 2023 daz-3ux(杨鹏达) <[email protected]>. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file. The original repo for
// this file is https://github.com/Daz-3ux/dBlog.

package user

import (
Expand Down
5 changes: 5 additions & 0 deletions internal/dazBlog/router.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// Copyright 2023 daz-3ux(杨鹏达) <[email protected]>. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file. The original repo for
// this file is https://github.com/Daz-3ux/dBlog.

package dazBlog

import (
Expand Down
15 changes: 14 additions & 1 deletion internal/pkg/model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@

package model

import "time"
import (
"github.com/Daz-3ux/dBlog/pkg/auth"
"gorm.io/gorm"
"time"
)

type UserM struct {
ID int64 `gorm:"column:id;primary_key"` // unique id for the user, server as the primary key
Expand All @@ -22,3 +26,12 @@ type UserM struct {
func (u *UserM) TableName() string {
return "users"
}

func (u *UserM) BeforeCreate(tx *gorm.DB) (err error) {
// Encrypt the user password
u.Password, err = auth.Encrypt(u.Password)
if err != nil {
return err
}
return nil
}
17 changes: 17 additions & 0 deletions pkg/auth/authn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2023 daz-3ux(杨鹏达) <[email protected]>. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file. The original repo for
// this file is https://github.com/Daz-3ux/dBlog.

package auth

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

func Encrypt(password string) (string, error) {
hashedBytes, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
return string(hashedBytes), err
}

func Compare(hashedPassword, password string) error {
return bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password))
}

0 comments on commit 162a4ac

Please sign in to comment.