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