Skip to content

Commit

Permalink
expression: Support GBK for builtin function AesEncrypt. (#29946)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackwener authored Nov 29, 2021
1 parent 6efa36d commit b1ca2eb
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
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 @@ -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;
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 @@ -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;
2 changes: 1 addition & 1 deletion expression/builtin_convert_charset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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{}{"你好"}, "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) {
Expand Down

0 comments on commit b1ca2eb

Please sign in to comment.