2022-02-01 09:44:42 +01:00

41 lines
809 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 {
if a.EncryptedPassword == "" {
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))
}
func NewAccount(login string, password string) *Account {
a := &Account{
Login: login,
Password: password,
}
a.EncryptPassword()
return a
}