]> Cypherpunks.ru repositories - gocheese.git/blob - auth_argon2i.go
More convenient trusted-host
[gocheese.git] / auth_argon2i.go
1 // GoCheese -- Python private package repository and caching proxy
2 // Copyright (C) 2019-2024 Elena Balakhonova <balakhonova_e@riseup.net>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, version 3 of the License.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 package main
17
18 import (
19         "bytes"
20         "encoding/base64"
21         "errors"
22         "fmt"
23         "strings"
24
25         "golang.org/x/crypto/argon2"
26 )
27
28 type Argon2iAuthData struct {
29         version  int
30         memory   uint32
31         time     uint32
32         threads  uint8
33         salt     []byte
34         password []byte
35 }
36
37 func (ad Argon2iAuthData) Auth(password string) bool {
38         hashedPassword := argon2.Key(
39                 []byte(password),
40                 ad.salt,
41                 ad.time,
42                 ad.memory,
43                 ad.threads,
44                 uint32(len(ad.password)),
45         )
46         return bytes.Equal(hashedPassword, ad.password)
47 }
48
49 func parseArgon2i(params string) (Auther, error) {
50         var time, memory uint32
51         var threads uint8
52         var version int
53         var saltAndPasswordUnitedB64 string
54         n, err := fmt.Sscanf(
55                 params,
56                 "v=%d$m=%d,t=%d,p=%d$%s",
57                 &version,
58                 &memory,
59                 &time,
60                 &threads,
61                 &saltAndPasswordUnitedB64,
62         )
63         if n != 5 || err != nil {
64                 return nil, fmt.Errorf("argon2i parameters %q have wrong format", params)
65         }
66         if version != argon2.Version {
67                 return nil, errors.New("unsupported argon2i version")
68         }
69         saltAndPasswordSplittedB64 := strings.Split(saltAndPasswordUnitedB64, "$")
70         salt, err := base64.RawStdEncoding.DecodeString(saltAndPasswordSplittedB64[0])
71         if err != nil {
72                 return nil, errors.New("invalid salt format")
73         }
74         password, err := base64.RawStdEncoding.DecodeString(saltAndPasswordSplittedB64[1])
75         if err != nil {
76                 return nil, errors.New("invalid password format")
77         }
78         return Argon2iAuthData{
79                 version:  version,
80                 time:     time,
81                 memory:   memory,
82                 threads:  threads,
83                 salt:     salt,
84                 password: password,
85         }, nil
86 }