2022-02-01 09:41:47 +01:00

30 lines
621 B
Go

package models
import (
"crypto"
"encoding/base64"
"time"
"gorm.io/gorm"
)
type Account struct {
ID uint32 `gorm:"primary_key"`
Login string `gorm:"unique;size:64;not null"`
Password string `gorm:"-"`
EncryptedPassword string `gorm:"size:44;not null"`
CreatedAt time.Time
UpdatedAt time.Time
}
func (a *Account) BeforeCreate(tx *gorm.DB) error {
a.EncryptPassword()
return nil
}
func (a *Account) EncryptPassword() {
sha1 := crypto.SHA256.New()
sha1.Write([]byte(a.Password))
a.EncryptedPassword = base64.StdEncoding.EncodeToString(sha1.Sum(nil))
}