Source File
	pbkdf2.go
Belonging Package
	github.com/jcmturner/gofork/x/crypto/pbkdf2
// Copyright 2012 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 pbkdf2 implements the key derivation function PBKDF2 as defined in RFC2898 / PKCS #5 v2.0.A key derivation function is useful when encrypting data based on a passwordor any other not-fully-random data. It uses a pseudorandom function to derivea secure encryption key based on the password.While v2.0 of the standard defines only one pseudorandom function to use,HMAC-SHA1, the drafted v2.1 specification allows use of all five FIPS ApprovedHash Functions SHA-1, SHA-224, SHA-256, SHA-384 and SHA-512 for HMAC. Tochoose, you can pass the `New` functions from the different SHA packages topbkdf2.Key.*/package pbkdf2import ()// Key derives a key from the password, salt and iteration count, returning a// []byte of length keylen that can be used as cryptographic key. The key is// derived based on the method described as PBKDF2 with the HMAC variant using// the supplied hash function.//// For example, to use a HMAC-SHA-1 based PBKDF2 key derivation function, you// can get a derived key for e.g. AES-256 (which needs a 32-byte key) by// doing://// dk := pbkdf2.Key([]byte("some password"), salt, 4096, 32, sha1.New)//// Remember to get a good random salt. At least 8 bytes is recommended by the// RFC.//// Using a higher iteration count will increase the cost of an exhaustive// search but will also make derivation proportionally slower.func (, []byte, , int, func() hash.Hash) []byte {return Key64(, , int64(), int64(), )}// Key64 derives a key from the password, salt and iteration count, returning a// []byte of length keylen that can be used as cryptographic key. Key64 uses// int64 for the iteration count and key length to allow larger values.// The key is derived based on the method described as PBKDF2 with the HMAC// variant using the supplied hash function.//// For example, to use a HMAC-SHA-1 based PBKDF2 key derivation function, you// can get a derived key for e.g. AES-256 (which needs a 32-byte key) by// doing://// dk := pbkdf2.Key([]byte("some password"), salt, 4096, 32, sha1.New)//// Remember to get a good random salt. At least 8 bytes is recommended by the// RFC.//// Using a higher iteration count will increase the cost of an exhaustive// search but will also make derivation proportionally slower.func (, []byte, , int64, func() hash.Hash) []byte {:= hmac.New(, ):= int64(.Size()):= ( + - 1) /var [4]byte:= make([]byte, 0, *):= make([]byte, )for := int64(1); <= ; ++ {// N.B.: || means concatenation, ^ means XOR// for each block T_i = U_1 ^ U_2 ^ ... ^ U_iter// U_1 = PRF(password, salt || uint(i)).Reset().Write()[0] = byte( >> 24)[1] = byte( >> 16)[2] = byte( >> 8)[3] = byte().Write([:4])= .Sum():= [int64(len())-:]copy(, )// U_n = PRF(password, U_(n-1))for := int64(2); <= ; ++ {.Reset().Write()= [:0]= .Sum()for := range {[] ^= []}}}return [:]}
![]()  | 
The pages are generated with Golds v0.6.7. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |