github.com/zntrio/harp/v2@v2.0.9/pkg/sdk/security/crypto/rfc6979/rfc6979_test.go (about)

     1  // Licensed to Elasticsearch B.V. under one or more contributor
     2  // license agreements. See the NOTICE file distributed with
     3  // this work for additional information regarding copyright
     4  // ownership. Elasticsearch B.V. licenses this file to you under
     5  // the Apache License, Version 2.0 (the "License"); you may
     6  // not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing,
    12  // software distributed under the License is distributed on an
    13  // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    14  // KIND, either express or implied.  See the License for the
    15  // specific language governing permissions and limitations
    16  // under the License.
    17  
    18  package rfc6979
    19  
    20  // From https://github.com/codahale/rfc6979
    21  
    22  import (
    23  	"crypto/sha256"
    24  	"encoding/hex"
    25  	"math/big"
    26  	"testing"
    27  )
    28  
    29  // https://tools.ietf.org/html/rfc6979#appendix-A.1
    30  func TestGenerateSecret(t *testing.T) {
    31  	q, _ := new(big.Int).SetString("4000000000000000000020108A2E0CC0D99F8A5EF", 16)
    32  	x, _ := new(big.Int).SetString("09A4D6792295A7F730FC3F2B49CBC0F62E862272F", 16)
    33  	hash, _ := hex.DecodeString("AF2BDBE1AA9B6EC1E2ADE1D694F41FC71A831D0268E9891562113D8A62ADD1BF")
    34  
    35  	expected, _ := new(big.Int).SetString("23AF4074C90A02B3FE61D286D5C87F425E6BDD81B", 16)
    36  	var actual *big.Int
    37  	generateSecret(q, x, sha256.New, hash, func(k *big.Int) bool {
    38  		actual = k
    39  		return true
    40  	})
    41  
    42  	if actual.Cmp(expected) != 0 {
    43  		t.Errorf("Expected %x, got %x", expected, actual)
    44  	}
    45  }