github.com/enetx/g@v1.0.80/tests/string_hash_test.go (about)

     1  package g_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/enetx/g"
     7  )
     8  
     9  func TestStringMD5(t *testing.T) {
    10  	tests := []struct {
    11  		name string
    12  		s    g.String
    13  		want g.String
    14  	}{
    15  		{
    16  			name: "empty",
    17  			s:    g.NewString("").Hash().MD5(),
    18  			want: g.String("d41d8cd98f00b204e9800998ecf8427e"),
    19  		},
    20  		{
    21  			name: "hello",
    22  			s:    g.NewString("hello").Hash().MD5(),
    23  			want: g.String("5d41402abc4b2a76b9719d911017c592"),
    24  		},
    25  	}
    26  	for _, tt := range tests {
    27  		t.Run(tt.name, func(t *testing.T) {
    28  			if got := tt.s; got != tt.want {
    29  				t.Errorf("g.String.MD5() = %v, want %v", got, tt.want)
    30  			}
    31  		})
    32  	}
    33  }
    34  
    35  func TestStringSHA1(t *testing.T) {
    36  	s := g.NewString("Hello, world!")
    37  	expected := "943a702d06f34599aee1f8da8ef9f7296031d699"
    38  
    39  	actual := s.Hash().SHA1().Std()
    40  	if actual != expected {
    41  		t.Errorf("Expected %s, got %s", expected, actual)
    42  	}
    43  }
    44  
    45  func TestStringSHA256(t *testing.T) {
    46  	tests := []struct {
    47  		name string
    48  		s    g.String
    49  		want g.String
    50  	}{
    51  		{
    52  			"empty",
    53  			g.String(""),
    54  			"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
    55  		},
    56  		{"a", g.String("a"), "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb"},
    57  		{
    58  			"abc",
    59  			g.String("abc"),
    60  			"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
    61  		},
    62  		{
    63  			"message digest",
    64  			g.String("message digest"),
    65  			"f7846f55cf23e14eebeab5b4e1550cad5b509e3348fbc4efa3a1413d393cb650",
    66  		},
    67  		{
    68  			"secure hash algorithm",
    69  			g.String("secure hash algorithm"),
    70  			"f30ceb2bb2829e79e4ca9753d35a8ecc00262d164cc077080295381cbd643f0d",
    71  		},
    72  	}
    73  
    74  	for _, tt := range tests {
    75  		t.Run(tt.name, func(t *testing.T) {
    76  			if got := tt.s.Hash().SHA256(); got != tt.want {
    77  				t.Errorf("g.String.SHA256() = %v, want %v", got, tt.want)
    78  			}
    79  		})
    80  	}
    81  }
    82  
    83  func TestStringSHA512(t *testing.T) {
    84  	tests := []struct {
    85  		name string
    86  		s    g.String
    87  		want g.String
    88  	}{
    89  		{
    90  			"empty",
    91  			g.String(""),
    92  			"cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e",
    93  		},
    94  		{
    95  			"hello",
    96  			g.String("hello"),
    97  			"9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043",
    98  		},
    99  		{
   100  			"hello world",
   101  			g.String("hello world"),
   102  			"309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f",
   103  		},
   104  	}
   105  
   106  	for _, tt := range tests {
   107  		t.Run(tt.name, func(t *testing.T) {
   108  			if got := tt.s.Hash().SHA512(); got != tt.want {
   109  				t.Errorf("g.String.SHA512() = %v, want %v", got, tt.want)
   110  			}
   111  		})
   112  	}
   113  }