diff --git a/cmd/explaintest/r/new_character_set_builtin.result b/cmd/explaintest/r/new_character_set_builtin.result index c4fb7c8c7fca3..fa733d990ef1e 100644 --- a/cmd/explaintest/r/new_character_set_builtin.result +++ b/cmd/explaintest/r/new_character_set_builtin.result @@ -502,3 +502,23 @@ select concat(a, 0xb6fe) from t; Error 3854: Cannot convert string 'B6FE' from binary to utf8mb4 select concat(b, 0xe4ba8c) from t; Error 3854: Cannot convert string 'E4BA8C' from binary to gbk +drop table if exists t; +create table t (a char(20) charset utf8mb4, b char(20) charset gbk, c binary(20)); +insert into t values ('一二三', '一二三', '一二三'); +set @@block_encryption_mode='aes-128-ecb'; +select hex(aes_encrypt(a, '123')), hex(aes_encrypt(b, '123')), hex(aes_encrypt(c, '123')) from t; +hex(aes_encrypt(a, '123')) hex(aes_encrypt(b, '123')) hex(aes_encrypt(c, '123')) +C54279F381B0710E145E94106F03C94C 7A747EC6F1906276D036B1F3CE27BAAB A0E5E01289017B8A3691CCFBDE81A59ED4A9D5BF50A298D41287E395CDDCAD56 +set @@block_encryption_mode='aes-128-ofb'; +select hex(aes_encrypt(a, '123', '1234567890123456')), hex(aes_encrypt(b, '123', '1234567890123456')), hex(aes_encrypt(c, '123', '1234567890123456')) from t; +hex(aes_encrypt(a, '123', '1234567890123456')) hex(aes_encrypt(b, '123', '1234567890123456')) hex(aes_encrypt(c, '123', '1234567890123456')) +65473346EE2BF64CD2 5344055C9C5A 65473346EE2BF64CD2BA02F128114B0E061E095B +set @@tidb_enable_vectorized_expression = true; +select hex(aes_encrypt(a, '123', '1234567890123456')), hex(aes_encrypt(b, '123', '1234567890123456')), hex(aes_encrypt(c, '123', '1234567890123456')) from t; +hex(aes_encrypt(a, '123', '1234567890123456')) hex(aes_encrypt(b, '123', '1234567890123456')) hex(aes_encrypt(c, '123', '1234567890123456')) +65473346EE2BF64CD2 5344055C9C5A 65473346EE2BF64CD2BA02F128114B0E061E095B +set @@block_encryption_mode='aes-128-ecb'; +select hex(aes_encrypt(a, '123')), hex(aes_encrypt(b, '123')), hex(aes_encrypt(c, '123')) from t; +hex(aes_encrypt(a, '123')) hex(aes_encrypt(b, '123')) hex(aes_encrypt(c, '123')) +C54279F381B0710E145E94106F03C94C 7A747EC6F1906276D036B1F3CE27BAAB A0E5E01289017B8A3691CCFBDE81A59ED4A9D5BF50A298D41287E395CDDCAD56 +set @@tidb_enable_vectorized_expression = false; diff --git a/cmd/explaintest/t/new_character_set_builtin.test b/cmd/explaintest/t/new_character_set_builtin.test index fd92ed513f018..09b823cdcfaa9 100644 --- a/cmd/explaintest/t/new_character_set_builtin.test +++ b/cmd/explaintest/t/new_character_set_builtin.test @@ -224,3 +224,17 @@ select b = 0xe4ba8c from t; select concat(a, 0xb6fe) from t; --error ER_CANNOT_CONVERT_STRING select concat(b, 0xe4ba8c) from t; + +-- test for builtin function aes_encrypt() +drop table if exists t; +create table t (a char(20) charset utf8mb4, b char(20) charset gbk, c binary(20)); +insert into t values ('一二三', '一二三', '一二三'); +set @@block_encryption_mode='aes-128-ecb'; +select hex(aes_encrypt(a, '123')), hex(aes_encrypt(b, '123')), hex(aes_encrypt(c, '123')) from t; +set @@block_encryption_mode='aes-128-ofb'; +select hex(aes_encrypt(a, '123', '1234567890123456')), hex(aes_encrypt(b, '123', '1234567890123456')), hex(aes_encrypt(c, '123', '1234567890123456')) from t; +set @@tidb_enable_vectorized_expression = true; +select hex(aes_encrypt(a, '123', '1234567890123456')), hex(aes_encrypt(b, '123', '1234567890123456')), hex(aes_encrypt(c, '123', '1234567890123456')) from t; +set @@block_encryption_mode='aes-128-ecb'; +select hex(aes_encrypt(a, '123')), hex(aes_encrypt(b, '123')), hex(aes_encrypt(c, '123')) from t; +set @@tidb_enable_vectorized_expression = false; diff --git a/expression/builtin_convert_charset.go b/expression/builtin_convert_charset.go index aac9d1680ddda..4101d00a3b66b 100644 --- a/expression/builtin_convert_charset.go +++ b/expression/builtin_convert_charset.go @@ -271,7 +271,7 @@ func HandleBinaryLiteral(ctx sessionctx.Context, expr Expression, ec *ExprCollat ft.Charset, ft.Collate = ec.Charset, ec.Collation return BuildFromBinaryFunction(ctx, expr, ft) } - case ast.Hex, ast.Length, ast.OctetLength, ast.ASCII, ast.ToBase64, ast.AesDecrypt, ast.Decode, ast.Encode, + case ast.Hex, ast.Length, ast.OctetLength, ast.ASCII, ast.ToBase64, ast.AesEncrypt, ast.AesDecrypt, ast.Decode, ast.Encode, ast.PasswordFunc, ast.MD5, ast.SHA, ast.SHA1, ast.SHA2, ast.Compress: if _, err := charset.GetDefaultCollationLegacy(expr.GetType().Charset); err != nil { return BuildToBinaryFunction(ctx, expr) diff --git a/expression/builtin_encryption_test.go b/expression/builtin_encryption_test.go index aab616c501b45..58f7095bc4330 100644 --- a/expression/builtin_encryption_test.go +++ b/expression/builtin_encryption_test.go @@ -164,6 +164,47 @@ func TestAESEncrypt(t *testing.T) { require.NoError(t, err) testNullInput(t, ctx, ast.AesEncrypt) testAmbiguousInput(t, ctx, ast.AesEncrypt) + + // Test GBK String + gbkStr, _ := charset.NewEncoding("gbk").EncodeString("你好") + gbkTests := []struct { + mode string + chs string + origin interface{} + params []interface{} + crypt string + }{ + // test for ecb + {"aes-128-ecb", "utf8mb4", "你好", []interface{}{"123"}, "CEBD80EEC6423BEAFA1BB30FD7625CBC"}, + {"aes-128-ecb", "gbk", gbkStr, []interface{}{"123"}, "6AFA9D7BA2C1AED1603E804F75BB0127"}, + {"aes-128-ecb", "utf8mb4", "123", []interface{}{"你好"}, "E03F6D9C1C86B82F5620EE0AA9BD2F6A"}, + {"aes-128-ecb", "gbk", "123", []interface{}{"你好"}, "31A2D26529F0E6A38D406379ABD26FA5"}, + {"aes-128-ecb", "utf8mb4", "你好", []interface{}{"你好"}, "3E2D8211DAE17143F22C2C5969A35263"}, + {"aes-128-ecb", "gbk", gbkStr, []interface{}{"你好"}, "84982910338160D037615D283AD413DE"}, + // test for cbc + {"aes-128-cbc", "utf8mb4", "你好", []interface{}{"123", "1234567890123456"}, "B95509A516ACED59C3DF4EC41C538D83"}, + {"aes-128-cbc", "gbk", gbkStr, []interface{}{"123", "1234567890123456"}, "D4322D091B5DDE0DEB35B1749DA2483C"}, + {"aes-128-cbc", "utf8mb4", "123", []interface{}{"你好", "1234567890123456"}, "E19E86A9E78E523267AFF36261AD117D"}, + {"aes-128-cbc", "gbk", "123", []interface{}{"你好", "1234567890123456"}, "5A2F8F2C1841CC4E1D1640F1EA2A1A23"}, + {"aes-128-cbc", "utf8mb4", "你好", []interface{}{"你好", "1234567890123456"}, "B73637C73302C909EA63274C07883E71"}, + {"aes-128-cbc", "gbk", gbkStr, []interface{}{"你好", "1234567890123456"}, "61E13E9B00F2E757F4E925D3268227A0"}, + } + + for _, tt := range gbkTests { + err := ctx.GetSessionVars().SetSystemVar(variable.CharacterSetConnection, tt.chs) + require.NoError(t, err) + err = variable.SetSessionSystemVar(ctx.GetSessionVars(), variable.BlockEncryptionMode, tt.mode) + require.NoError(t, err) + + args := datumsToConstants([]types.Datum{types.NewDatum(tt.origin)}) + args = append(args, primitiveValsToConstants(ctx, tt.params)...) + f, err := fc.getFunction(ctx, args) + + require.NoError(t, err) + crypt, err := evalBuiltinFunc(f, chunk.Row{}) + require.NoError(t, err) + require.Equal(t, types.NewDatum(tt.crypt), toHex(crypt)) + } } func TestAESDecrypt(t *testing.T) {