Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expression: Support GBK for builtin function AesEncrypt. #29946

Merged
merged 17 commits into from
Nov 29, 2021
Merged
20 changes: 20 additions & 0 deletions cmd/explaintest/r/new_character_set_builtin.result
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,23 @@ select ord(a), ord(b), ord(c) from t;
ord(a) ord(b) ord(c)
14989485 54992 228
set @@tidb_enable_vectorized_expression = false;
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;
14 changes: 14 additions & 0 deletions cmd/explaintest/t/new_character_set_builtin.test
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,17 @@ select ord(a), ord(b), ord(c) from t;
set @@tidb_enable_vectorized_expression = true;
select ord(a), ord(b), ord(c) from t;
set @@tidb_enable_vectorized_expression = false;

-- 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;
2 changes: 1 addition & 1 deletion expression/builtin_convert_charset.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (b *builtinInternalToBinarySig) vecEvalString(input *chunk.Chunk, result *c
// toBinaryMap contains the builtin functions which arguments need to be converted to the correct charset.
var toBinaryMap = map[string]struct{}{
ast.Hex: {}, ast.Length: {}, ast.OctetLength: {}, ast.ASCII: {},
ast.ToBase64: {}, ast.AesDecrypt: {}, ast.Decode: {}, ast.Encode: {},
ast.ToBase64: {}, ast.AesDecrypt: {}, ast.AesEncrypt: {}, ast.Decode: {}, ast.Encode: {},
ast.PasswordFunc: {}, ast.MD5: {}, ast.SHA: {}, ast.SHA1: {},
ast.SHA2: {}, ast.Compress: {},
}
Expand Down
41 changes: 41 additions & 0 deletions expression/builtin_encryption_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}{gbkStr}, "31A2D26529F0E6A38D406379ABD26FA5"},
{"aes-128-ecb", "utf8mb4", "你好", []interface{}{"你好"}, "3E2D8211DAE17143F22C2C5969A35263"},
{"aes-128-ecb", "gbk", gbkStr, []interface{}{gbkStr}, "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{}{gbkStr, "1234567890123456"}, "5A2F8F2C1841CC4E1D1640F1EA2A1A23"},
Copy link
Contributor

@unconsolable unconsolable Nov 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please take a look of #30072, we'd better leave the conversion to to_binary. (But I'm not so sure, wait on reviewers' opinions.)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please refer to @unconsolable's PR to modify the usage of datumsToConstants.

{"aes-128-cbc", "utf8mb4", "你好", []interface{}{"你好", "1234567890123456"}, "B73637C73302C909EA63274C07883E71"},
{"aes-128-cbc", "gbk", gbkStr, []interface{}{gbkStr, "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 := []types.Datum{types.NewDatum(tt.origin)}
for _, param := range tt.params {
args = append(args, types.NewDatum(param))
}
f, err := fc.getFunction(ctx, datumsToConstants(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) {
Expand Down