github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/xcrypto/pkcs12/pbkdf_test.go (about)

     1  // Copyright 2015 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package pkcs12
     6  
     7  import (
     8  	"bytes"
     9  	"testing"
    10  )
    11  
    12  func TestThatPBKDFWorksCorrectlyForLongKeys(t *testing.T) {
    13  	cipherInfo := shaWithTripleDESCBC{}
    14  
    15  	salt := []byte("\xff\xff\xff\xff\xff\xff\xff\xff")
    16  	password, _ := bmpString("sesame")
    17  	key := cipherInfo.deriveKey(salt, password, 2048)
    18  
    19  	if expected := []byte("\x7c\xd9\xfd\x3e\x2b\x3b\xe7\x69\x1a\x44\xe3\xbe\xf0\xf9\xea\x0f\xb9\xb8\x97\xd4\xe3\x25\xd9\xd1"); bytes.Compare(key, expected) != 0 {
    20  		t.Fatalf("expected key '%x', but found '%x'", expected, key)
    21  	}
    22  }
    23  
    24  func TestThatPBKDFHandlesLeadingZeros(t *testing.T) {
    25  	// This test triggers a case where I_j (in step 6C) ends up with leading zero
    26  	// byte, meaning that len(Ijb) < v (leading zeros get stripped by big.Int).
    27  	// This was previously causing bug whereby certain inputs would break the
    28  	// derivation and produce the wrong output.
    29  	key := pbkdf(sha1Sum, 20, 64, []byte("\xf3\x7e\x05\xb5\x18\x32\x4b\x4b"), []byte("\x00\x00"), 2048, 1, 24)
    30  	expected := []byte("\x00\xf7\x59\xff\x47\xd1\x4d\xd0\x36\x65\xd5\x94\x3c\xb3\xc4\xa3\x9a\x25\x55\xc0\x2a\xed\x66\xe1")
    31  	if bytes.Compare(key, expected) != 0 {
    32  		t.Fatalf("expected key '%x', but found '%x'", expected, key)
    33  	}
    34  }