github.com/ncruces/go-sqlite3@v0.15.1-0.20240520133447-53eef1510ff0/ext/hash/hash_test.go (about)

     1  package hash
     2  
     3  import (
     4  	_ "crypto/md5"
     5  	_ "crypto/sha1"
     6  	_ "crypto/sha256"
     7  	_ "crypto/sha512"
     8  	"testing"
     9  
    10  	"github.com/ncruces/go-sqlite3"
    11  	"github.com/ncruces/go-sqlite3/driver"
    12  	_ "github.com/ncruces/go-sqlite3/embed"
    13  	_ "github.com/ncruces/go-sqlite3/tests/testcfg"
    14  	_ "golang.org/x/crypto/blake2b"
    15  	_ "golang.org/x/crypto/blake2s"
    16  	_ "golang.org/x/crypto/md4"
    17  	_ "golang.org/x/crypto/ripemd160"
    18  	_ "golang.org/x/crypto/sha3"
    19  )
    20  
    21  func TestRegister(t *testing.T) {
    22  	t.Parallel()
    23  
    24  	tests := []struct {
    25  		name string
    26  		hash string
    27  	}{
    28  		{"md4(NULL)", ""},
    29  		{"md4(X'')", "31D6CFE0D16AE931B73C59D7E0C089C0"},
    30  		{"md4('The quick brown fox jumps over the lazy dog')", "1BEE69A46BA811185C194762ABAEAE90"},
    31  
    32  		{"md5('')", "D41D8CD98F00B204E9800998ECF8427E"},
    33  		{"sha1('')", "DA39A3EE5E6B4B0D3255BFEF95601890AFD80709"},
    34  		{"ripemd160('')", "9C1185A5C5E9FC54612808977EE8F548B2258D31"},
    35  
    36  		{"sha224('')", "D14A028C2A3A2BC9476102BB288234C415A2B01F828EA62AC5B3E42F"},
    37  		{"sha256('')", "E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855"},
    38  		{"sha256('', 224)", "D14A028C2A3A2BC9476102BB288234C415A2B01F828EA62AC5B3E42F"},
    39  		{"sha384('')", "38B060A751AC96384CD9327EB1B1E36A21FDB71114BE07434C0CC7BF63F6E1DA274EDEBFE76F65FBD51AD2F14898B95B"},
    40  		{"sha512('')", "CF83E1357EEFB8BDF1542850D66D8007D620E4050B5715DC83F4A921D36CE9CE47D0D13C5D85F2B0FF8318D2877EEC2F63B931BD47417A81A538327AF927DA3E"},
    41  		{"sha512('', 224)", "6ED0DD02806FA89E25DE060C19D3AC86CABB87D6A0DDD05C333B84F4"},
    42  		{"sha512('', 256)", "C672B8D1EF56ED28AB87C3622C5114069BDD3AD7B8F9737498D0C01ECEF0967A"},
    43  		{"sha512('', 384)", "38B060A751AC96384CD9327EB1B1E36A21FDB71114BE07434C0CC7BF63F6E1DA274EDEBFE76F65FBD51AD2F14898B95B"},
    44  
    45  		{"sha3('')", "A7FFC6F8BF1ED76651C14756A061D662F580FF4DE43B49FA82D80A4B80F8434A"},
    46  		{"sha3('', 224)", "6B4E03423667DBB73B6E15454F0EB1ABD4597F9A1B078E3F5B5A6BC7"},
    47  		{"sha3('', 384)", "0C63A75B845E4F7D01107D852E4C2485C51A50AAAA94FC61995E71BBEE983A2AC3713831264ADB47FB6BD1E058D5F004"},
    48  		{"sha3('', 512)", "A69F73CCA23A9AC5C8B567DC185A756E97C982164FE25859E0D1DCC1475C80A615B2123AF1F5F94C11E3E9402C3AC558F500199D95B6D3E301758586281DCD26"},
    49  
    50  		{"blake2s('')", "69217A3079908094E11121D042354A7C1F55B6482CA1A51E1B250DFD1ED0EEF9"},
    51  		{"blake2b('')", "786A02F742015903C6C6FD852552D272912F4740E15847618A86E217F71F5419D25E1031AFEE585313896444934EB04B903A685B1448B755D56F701AFE9BE2CE"},
    52  		{"blake2b('', 384)", "B32811423377F52D7862286EE1A72EE540524380FDA1724A6F25D7978C6FD3244A6CAF0498812673C5E05EF583825100"},
    53  		{"blake2b('', 256)", "0E5751C026E543B2E8AB2EB06099DAA1D1E5DF47778F7787FAAB45CDF12FE3A8"},
    54  	}
    55  
    56  	db, err := driver.Open(":memory:", func(c *sqlite3.Conn) error {
    57  		Register(c)
    58  		return nil
    59  	})
    60  	if err != nil {
    61  		t.Fatal(err)
    62  	}
    63  	defer db.Close()
    64  
    65  	for _, tt := range tests {
    66  		t.Run(tt.name, func(t *testing.T) {
    67  			var hash string
    68  
    69  			err = db.QueryRow(`SELECT hex(` + tt.name + `)`).Scan(&hash)
    70  			if err != nil {
    71  				t.Fatal(err)
    72  			}
    73  
    74  			if hash != tt.hash {
    75  				t.Errorf("got %s, want %s", hash, tt.hash)
    76  			}
    77  		})
    78  	}
    79  
    80  	_, err = db.Exec(`SELECT sha256('', 255)`)
    81  	if err == nil {
    82  		t.Error("want error")
    83  	}
    84  
    85  	_, err = db.Exec(`SELECT sha512('', 255)`)
    86  	if err == nil {
    87  		t.Error("want error")
    88  	}
    89  
    90  	_, err = db.Exec(`SELECT sha3('', 255)`)
    91  	if err == nil {
    92  		t.Error("want error")
    93  	}
    94  
    95  	_, err = db.Exec(`SELECT blake2b('', 255)`)
    96  	if err == nil {
    97  		t.Error("want error")
    98  	}
    99  }