-/*
-balloon -- Balloon password hashing function
-Copyright (C) 2016-2021 Sergey Matveev <stargrave@stargrave.org>
-
-This program is free software: you can redistribute it and/or modify
-it under the terms of the GNU Lesser General Public License as
-the Free Software Foundation, version 3 of the License.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-GNU Lesser General Public License for more details.
-
-You should have received a copy of the GNU Lesser General Public
-License along with this program. If not, see
-<http://www.gnu.org/licenses/>.
-*/
+// balloon -- Balloon password hashing function
+// Copyright (C) 2016-2024 Sergey Matveev <stargrave@stargrave.org>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as
+// the Free Software Foundation, version 3 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this program. If not, see
+// <http://www.gnu.org/licenses/>.
// This function takes hash, password, salt, space cost (buffer size,
// number of hash-output sized blocks), time cost (number of rounds) and
// performs the following:
//
// This function takes hash, password, salt, space cost (buffer size,
// number of hash-output sized blocks), time cost (number of rounds) and
// performs the following:
//
-// # Expand input into buffer.
-// buf[0] = hash(cnt++ || passwd || salt)
-// for m from 1 to sCost-1:
-// buf[m] = hash(cnt++ || buf[m-1])
-// # Mix buffer contents.
-// for t from 0 to tCost-1:
-// for m from 0 to sCost-1:
-// # Hash last and current blocks.
-// prev = buf[(m-1) mod sCost]
-// buf[m] = hash(cnt++ || prev || buf[m])
-// # Hash in pseudorandomly chosen blocks.
-// for i from 0 to delta-1:
-// other = to_int(hash(cnt++ || salt || t || m || i)) mod sCost
-// buf[m] = hash(cnt++ || buf[m] || buf[other])
-// # Extract output from buffer.
-// return buf[sCost-1]
+// # Expand input into buffer.
+// buf[0] = hash(cnt++ || passwd || salt)
+// for m from 1 to sCost-1:
+// buf[m] = hash(cnt++ || buf[m-1])
+// # Mix buffer contents.
+// for t from 0 to tCost-1:
+// for m from 0 to sCost-1:
+// # Hash last and current blocks.
+// prev = buf[(m-1) mod sCost]
+// buf[m] = hash(cnt++ || prev || buf[m])
+// # Hash in pseudorandomly chosen blocks.
+// for i from 0 to delta-1:
+// other = to_int(hash(cnt++ || salt || t || m || i)) mod sCost
+// buf[m] = hash(cnt++ || buf[m] || buf[other])
+// # Extract output from buffer.
+// return buf[sCost-1]
func B(h hash.Hash, passwd, salt []byte, sCost, tCost uint64) []byte {
var cnt uint64
intBuf := make([]byte, 8)
hSize := uint64(h.Size())
buf := make([]byte, 0, sCost*hSize)
// Expand input into buffer
func B(h hash.Hash, passwd, salt []byte, sCost, tCost uint64) []byte {
var cnt uint64
intBuf := make([]byte, 8)
hSize := uint64(h.Size())
buf := make([]byte, 0, sCost*hSize)
// Expand input into buffer
+ for i, j := 0, len(biBuf)-1; i < j; i, j = i+1, j-1 {
+ biBuf[i], biBuf[j] = biBuf[j], biBuf[i]
+ }
// run several hashers (jobs) simultaneously and second-preimage resistant
// password double hashing.
//
// run several hashers (jobs) simultaneously and second-preimage resistant
// password double hashing.
//
func H(hasher func() hash.Hash, passwd, salt []byte, sCost, tCost int, jobs int) []byte {
var i int
results := make(chan []byte)
func H(hasher func() hash.Hash, passwd, salt []byte, sCost, tCost int, jobs int) []byte {
var i int
results := make(chan []byte)
results <- B(hasher(), passwd, saltBuf, uint64(sCost), uint64(tCost))
}(i)
}
results <- B(hasher(), passwd, saltBuf, uint64(sCost), uint64(tCost))
}(i)
}