github.com/tommi2day/gomodules@v1.13.2-0.20240423190010-b7d55d252a27/pwlib/scram_test.go (about)

     1  package pwlib
     2  
     3  // Origin: https://github.com/tv42/scram-password/tree/main/internal/scramble
     4  // License: https://github.com/tv42/scram-password/blob/main/LICENSE
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  
    11  	"github.com/xdg-go/scram"
    12  )
    13  
    14  func TestScram(t *testing.T) {
    15  	salt := [32]byte{
    16  		0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
    17  		0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
    18  		0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
    19  		0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
    20  	}
    21  	hash, err := hashWithKF("jdoe", "s3kr1t", scram.KeyFactors{
    22  		Iters: 4096,
    23  		Salt:  string(salt[:]),
    24  	})
    25  	if err != nil {
    26  		t.Fatal(err)
    27  	}
    28  	// nolint: gosec
    29  	const want = `SCRAM-SHA-256$4096:AAECAwQFBgcAAQIDBAUGBwABAgMEBQYHAAECAwQFBgc=$3OKulhqxk9w6FbPtpUHCuIkEsW+2F2cjX0/ABNgYsbI=:BZ55glbzmkm4V5VjvpHHENWSEZE/IVxZWuAqeLUsikQ=`
    30  	if g, e := hash, want; g != e {
    31  		t.Errorf("wrong hash:\n\tgot\t%s\n\twant\t%s\n", g, e)
    32  	}
    33  }
    34  
    35  func TestScramPassword(t *testing.T) {
    36  	username := "testuser"
    37  	password := "verySecret"
    38  	actual, err := ScramPassword(username, password)
    39  	assert.NoError(t, err, "should not return error:%s", err)
    40  	assert.NotEmpty(t, actual, "Value should not be empty")
    41  	t.Log(actual)
    42  }