mirror of
https://github.com/Luzifer/cloudkeys-go.git
synced 2024-11-14 00:42:44 +00:00
Knut Ahlers
a1df72edc5
commitf0db1ff1f8
Author: Knut Ahlers <knut@ahlers.me> Date: Sun Dec 24 12:19:56 2017 +0100 Mark option as deprecated Signed-off-by: Knut Ahlers <knut@ahlers.me> commit9891df2a16
Author: Knut Ahlers <knut@ahlers.me> Date: Sun Dec 24 12:11:56 2017 +0100 Fix: Typo Signed-off-by: Knut Ahlers <knut@ahlers.me> commit836006de64
Author: Knut Ahlers <knut@ahlers.me> Date: Sun Dec 24 12:04:20 2017 +0100 Add new dependencies Signed-off-by: Knut Ahlers <knut@ahlers.me> commitd64fee60c8
Author: Knut Ahlers <knut@ahlers.me> Date: Sun Dec 24 11:55:52 2017 +0100 Replace insecure password hashing Prior this commit passwords were hashed with a static salt and using the SHA1 hashing function. This could lead to passwords being attackable in case someone gets access to the raw data stored inside the database. This commit introduces password hashing using bcrypt hashing function which addresses this issue. Old passwords are not automatically re-hashed as they are unknown. Replacing the old password scheme is not that easy and needs #10 to be solved. Therefore the old hashing scheme is kept for compatibility reason. Signed-off-by: Knut Ahlers <knut@ahlers.me> Signed-off-by: Knut Ahlers <knut@ahlers.me> closes #14 closes #15
61 lines
1.5 KiB
Go
61 lines
1.5 KiB
Go
// Copyright 2014 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package hkdf_test
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/rand"
|
|
"crypto/sha256"
|
|
"fmt"
|
|
"golang.org/x/crypto/hkdf"
|
|
"io"
|
|
)
|
|
|
|
// Usage example that expands one master key into three other cryptographically
|
|
// secure keys.
|
|
func Example_usage() {
|
|
// Underlying hash function to use
|
|
hash := sha256.New
|
|
|
|
// Cryptographically secure master key.
|
|
master := []byte{0x00, 0x01, 0x02, 0x03} // i.e. NOT this.
|
|
|
|
// Non secret salt, optional (can be nil)
|
|
// Recommended: hash-length sized random
|
|
salt := make([]byte, hash().Size())
|
|
n, err := io.ReadFull(rand.Reader, salt)
|
|
if n != len(salt) || err != nil {
|
|
fmt.Println("error:", err)
|
|
return
|
|
}
|
|
|
|
// Non secret context specific info, optional (can be nil).
|
|
// Note, independent from the master key.
|
|
info := []byte{0x03, 0x14, 0x15, 0x92, 0x65}
|
|
|
|
// Create the key derivation function
|
|
hkdf := hkdf.New(hash, master, salt, info)
|
|
|
|
// Generate the required keys
|
|
keys := make([][]byte, 3)
|
|
for i := 0; i < len(keys); i++ {
|
|
keys[i] = make([]byte, 24)
|
|
n, err := io.ReadFull(hkdf, keys[i])
|
|
if n != len(keys[i]) || err != nil {
|
|
fmt.Println("error:", err)
|
|
return
|
|
}
|
|
}
|
|
|
|
// Keys should contain 192 bit random keys
|
|
for i := 1; i <= len(keys); i++ {
|
|
fmt.Printf("Key #%d: %v\n", i, !bytes.Equal(keys[i-1], make([]byte, 24)))
|
|
}
|
|
|
|
// Output:
|
|
// Key #1: true
|
|
// Key #2: true
|
|
// Key #3: true
|
|
}
|