MCPcopy
hub / github.com/syncthing/syncthing / KeyFromPassword

Method KeyFromPassword

lib/protocol/encryption.go:563–581  ·  view source on GitHub ↗

KeyFromPassword uses key derivation to generate a stronger key from a probably weak password.

(folderID, password string)

Source from the content-addressed store, hash-verified

561// KeyFromPassword uses key derivation to generate a stronger key from a
562// probably weak password.
563func (g *KeyGenerator) KeyFromPassword(folderID, password string) *[keySize]byte {
564 cacheKey := folderKeyCacheKey{folderID, password}
565 g.mut.Lock()
566 defer g.mut.Unlock()
567 if key, ok := g.folderKeys.Get(cacheKey); ok {
568 return key
569 }
570 bs, err := scrypt.Key([]byte(password), knownBytes(folderID), 32768, 8, 1, keySize)
571 if err != nil {
572 panic("key derivation failure: " + err.Error())
573 }
574 if len(bs) != keySize {
575 panic("key derivation failure: wrong number of bytes")
576 }
577 var key [keySize]byte
578 copy(key[:], bs)
579 g.folderKeys.Add(cacheKey, &key)
580 return &key
581}
582
583var hkdfSalt = []byte("syncthing")
584

Callers 4

TestKeyDerivationFunction · 0.80
keysFromPasswordsFunction · 0.80
PasswordTokenFunction · 0.80
RunMethod · 0.80

Calls 6

knownBytesFunction · 0.85
UnlockMethod · 0.80
KeyMethod · 0.80
GetMethod · 0.65
ErrorMethod · 0.65
AddMethod · 0.45

Tested by 1

TestKeyDerivationFunction · 0.64