编解码


编解码类

  • ascii(string str)

    • 返回字符串中的 “第一个字符” 的 ASCII 码值

    • select ascii('a'), ascii('apple'), ascii('测'), ascii('试验');
      
  • chr(bigint|double A)

    • 将 ASCII 码解析为字符

    • 支持标准 ASCII 和扩展 ASCII

    • 如果输入的数值大于等于 256 ,则使用该数值对 256 取模

    • select chr(97), chr(255), chr(256+65), ascii(chr(256));
      
  • base64(binary bin)

    • 将 binary 转换为 base64 编码

    • select
          base64(1001),	-- 报错
          base64('1001');	-- 报错
      
      drop table if exists test.test;
      create table IF NOT EXISTS test.test(
          bin BINARY
      );
      insert into test.test values
      (114),
      ('test test'),
      ('测试');
      select bin, base64(bin) from test.test;
      
  • unbase64(string str)

    • 将 base64 编码解析为字符串

    • select
          unbase64('MTE0'),
          unbase64('dGVzdCB0ZXN0'),
          unbase64('5rWL6K+V');
      

    045

  • encode(string src, string charset)

    • 返回类型: binary

    • 将字符串按照指定编码方案进行编码后返回

    • 支持的编码方案有:‘US-ASCII’, ‘ISO-8859-1’,‘UTF-8’, ‘UTF-16BE’, ‘UTF-16LE’, and ‘UTF-16’

    • 任何一个参数为 NULL ,则报错

    • select encode('test', 'UTF-8'), encode('测试', 'UTF-8');
      
  • decode(binary bin, string charset)

    • 返回类型: string

    • 将 binary 按照指定的编码方案进行解码后返回

    • drop table if exists test.test;
      create table IF NOT EXISTS test.test(
          bin BINARY
      );
      insert into test.test values
      (114),
      ('test test'),
      ('测试');
      select bin, base64(bin) from test.test;
      select bin, decode(bin, 'UTF-8') from test.test;
      

    046

  • 上面这两个可以与 base64()unbase64() 配合使用

    • select base64(encode('这样能避免遇到乱码问题', 'utf8'));
      drop table if exists test.test;
      create table IF NOT EXISTS test.test(
          bin BINARY
      );
      insert into test.test values
      ('6L+Z5qC36IO96YG/5YWN6YGH5Yiw5Lmx56CB6Zeu6aKY');
      select bin, unbase64(decode(bin, 'UTF-8')) from test.test;
      

    047

  • soundex(string A)

    • 返回单词的 Soundex 编码

    • Soundex 编码方法根据单词的拼写将单词进行分组,使得同一组的单词发音很接近

    • select
          soundex('Miller'),
          soundex('Alan Mathison Turing'),
          soundex('Alan');
      
  • aes_encrypt(input string/binary, key string/binary)

    • 使用 AES 加密 string 或 binary

    • 密钥长度 128 位

      • 即 16 个 ASCII 字符(不支持扩展 ASCII)
      • 如果安装了 Java Cryptography Extension ( JCE ) Unlimited Strength Jurisdiction Policy Files ,可使用 192 ,256 位密钥
    • 如果任一参数为 NULL 或密钥长度不是允许的值,则返回 NULL

    • select
          aes_encrypt('This is a secret', '1234567890123456'),
          aes_encrypt('This is a secret', 'aB|-.(1234567890'),
          base64(aes_encrypt('This is a secret', 'aB|-.(1234567890'));
      
  • aes_decrypt(input binary, key string/binary)

    • 使用 AES 解密

    • select
          aes_decrypt(unbase64('WPl945SuyeXWfLppN+bA4RLWZuVFHys+KqpwerxgkBQ='), 'aB|-.(1234567890');
      

    048


Hash 类

  • hash(a1[, a2...])

  • crc32(string/binary)

    • 返回 crc32 哈希

    • select crc32('text'), crc32('测试'), crc32(NULL);
      
  • md5(string/binary)

    • 返回 md5 哈希

    • select md5('text'), md5('测试'), md5(NULL);
      
  • sha1(string/binary)sha(string/binary)

    • 返回 sha1 哈希

    • select sha1('text'), sha1('测试'), sha1(NULL);
      
  • sha2(string/binary, int)

    • 返回 sha2 哈希

    • 第二个参数指定具体算法(位数)

      • 224(SHA-224)、256 或 0(SHA-256)、384(SHA-384)、512(SHA-512)
    • 若第二个参数不是合法的位数,则返回 NULL

    • select
          -- sha2('text'), --  sha2 requires 2 argument(s)
          -- sha2(NULL, 244), -- sha2 only takes STRING_GROUP, BINARY_GROUP types as 1st argument
          -- sha2('', NULL), -- sha2 only takes NUMERIC_GROUP types as 2nd argument
          sha2('', 222);
      select sha2('test', 224);
      select sha2('', 0);
      select sha2('测试', 256);
      

    054