]> Cypherpunks.ru repositories - balloon.git/blob - cmd/balloon/main.go
Unify copyright comment format
[balloon.git] / cmd / balloon / main.go
1 // balloon -- Balloon password hashing function
2 // Copyright (C) 2016-2024 Sergey Matveev <stargrave@stargrave.org>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Lesser General Public License as
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 Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this program.  If not, see
15 // <http://www.gnu.org/licenses/>.
16
17 package main
18
19 import (
20         "crypto/rand"
21         "crypto/sha512"
22         "encoding/base64"
23         "encoding/hex"
24         "flag"
25         "fmt"
26         "io"
27         "os"
28
29         "go.cypherpunks.ru/balloon/v2"
30 )
31
32 func main() {
33         s := flag.Int("s", 1<<16, "Space cost, number of hash-sized blocks")
34         t := flag.Int("t", 3, "Time cost, rounds")
35         p := flag.Int("p", 1, "Number of threads")
36         saltHex := flag.String("salt", "", "Salt, hexadecimal, optional")
37         passwd := flag.String("passwd", "", "Password")
38         flag.Usage = func() {
39                 fmt.Fprintf(os.Stderr, "balloon -- Strengthen password with Balloon+SHA512\n\n")
40                 flag.PrintDefaults()
41         }
42         flag.Parse()
43         var salt []byte
44         var err error
45         if len(*saltHex) == 0 {
46                 salt = make([]byte, 8)
47                 _, err = io.ReadFull(rand.Reader, salt)
48         } else {
49                 salt, err = hex.DecodeString(*saltHex)
50         }
51         if err != nil {
52                 panic(err)
53         }
54         fmt.Println("Salt:", hex.EncodeToString(salt))
55         h := balloon.H(sha512.New, []byte(*passwd), salt, *s, *t, *p)
56         fmt.Println("Hash:", hex.EncodeToString(h))
57         fmt.Printf(
58                 "Encoded: $balloon$h=sha512,s=%d,t=%d,p=%d$%s$%s\n",
59                 *s, *t, *p,
60                 base64.RawStdEncoding.EncodeToString(salt),
61                 base64.RawStdEncoding.EncodeToString(h),
62         )
63 }