Debian Codegraph @

yescrypt/yescrypt_wrapper.go

raw symbols SCIP
1 // Copyright 2024 Solar Designer. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // Alternatively, this specific source file is also available under more
6 // relaxed terms (0-clause BSD license):
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted.
9
10 // yescrypt support sponsored by Sandfly Security https://sandflysecurity.com -
11 // Agentless Security for Linux
12
13 package yescrypt
14
15 import (
16 "bytes"
17 "errors"
18 )
19
20 // Hash computes yescrypt hash encoding given the password and existing yescrypt
21 // setting or full hash encoding. The salt and other parameters are decoded
22 // from setting. Currently supports (only a little more than) the subset of
23 // yescrypt parameters that libxcrypt can generate (as of libxcrypt 4.4.36).
24 func Hash(password, setting []byte) ([]byte, error) {
25 if len(setting) < 7 || string(setting[:4]) != "$y$j" || setting[6] != '$' {
26 return nil, errors.New("yescrypt: unsupported parameters")
27 }
28
29 // Proper yescrypt uses variable-length integers
30 // We take a shortcut approach that works in a more limited range
31 Nlog2 := atoi64(setting[4]) + 1
32 if Nlog2 < 10 || Nlog2 > 18 {
33 return nil, errors.New("yescrypt: N out of supported range")
34 }
35
36 r := atoi64(setting[5]) + 1
37 if r < 1 || r > 32 {
38 return nil, errors.New("yescrypt: r out of supported range")
39 }
40
41 saltEnd := bytes.LastIndexByte(setting, '$')
42 if saltEnd < 7 {
43 saltEnd = len(setting)
44 }
45
46 salt := Decode64(setting[7:saltEnd])
47 if salt == nil {
48 return nil, errors.New("yescrypt: bad salt encoding")
49 }
50
51 key, err := Key(password, salt, 1<<Nlog2, r, 1, 32)
52 if err != nil {
53 return nil, err
54 }
55
56 hash := Encode64(key)
57
58 return bytes.Join([][]byte{setting[0:saltEnd], hash}, []byte("$")), nil
59 }