<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h2 class="title">
      <a name="encryption-functions">
      </a>
      14.13 Encryption and Compression Functions
     </h2>
    </div>
   </div>
  </div>
  <a class="indexterm" name="idm46045199614576">
  </a>
  <a class="indexterm" name="idm46045199613536">
  </a>
  <div class="table">
   <a name="idm46045199612048">
   </a>
   <p class="title">
    <b>
     Table 14.18 Encryption Functions
    </b>
   </p>
   <div class="table-contents">
    <table frame="box" rules="all" summary="A reference that lists encryption functions.">
     <colgroup>
      <col style="width: 28%"/>
      <col style="width: 71%"/>
     </colgroup>
     <thead>
      <tr>
       <th>
        Name
       </th>
       <th>
        Description
       </th>
      </tr>
     </thead>
     <tbody>
      <tr>
       <td>
        <a class="link" href="encryption-functions.html#function_aes-decrypt">
         <code class="literal">
          AES_DECRYPT()
         </code>
        </a>
       </td>
       <td>
        Decrypt using AES
       </td>
      </tr>
      <tr>
       <td>
        <a class="link" href="encryption-functions.html#function_aes-encrypt">
         <code class="literal">
          AES_ENCRYPT()
         </code>
        </a>
       </td>
       <td>
        Encrypt using AES
       </td>
      </tr>
      <tr>
       <td>
        <a class="link" href="encryption-functions.html#function_compress">
         <code class="literal">
          COMPRESS()
         </code>
        </a>
       </td>
       <td>
        Return result as a binary string
       </td>
      </tr>
      <tr>
       <td>
        <a class="link" href="encryption-functions.html#function_md5">
         <code class="literal">
          MD5()
         </code>
        </a>
       </td>
       <td>
        Calculate MD5 checksum
       </td>
      </tr>
      <tr>
       <td>
        <a class="link" href="encryption-functions.html#function_random-bytes">
         <code class="literal">
          RANDOM_BYTES()
         </code>
        </a>
       </td>
       <td>
        Return a random byte vector
       </td>
      </tr>
      <tr>
       <td>
        <a class="link" href="encryption-functions.html#function_sha1">
         <code class="literal">
          SHA1()
         </code>
         ,
         <code class="literal">
          SHA()
         </code>
        </a>
       </td>
       <td>
        Calculate an SHA-1 160-bit checksum
       </td>
      </tr>
      <tr>
       <td>
        <a class="link" href="encryption-functions.html#function_sha2">
         <code class="literal">
          SHA2()
         </code>
        </a>
       </td>
       <td>
        Calculate an SHA-2 checksum
       </td>
      </tr>
      <tr>
       <td>
        <a class="link" href="encryption-functions.html#function_statement-digest">
         <code class="literal">
          STATEMENT_DIGEST()
         </code>
        </a>
       </td>
       <td>
        Compute statement digest hash value
       </td>
      </tr>
      <tr>
       <td>
        <a class="link" href="encryption-functions.html#function_statement-digest-text">
         <code class="literal">
          STATEMENT_DIGEST_TEXT()
         </code>
        </a>
       </td>
       <td>
        Compute normalized statement digest
       </td>
      </tr>
      <tr>
       <td>
        <a class="link" href="encryption-functions.html#function_uncompress">
         <code class="literal">
          UNCOMPRESS()
         </code>
        </a>
       </td>
       <td>
        Uncompress a string compressed
       </td>
      </tr>
      <tr>
       <td>
        <a class="link" href="encryption-functions.html#function_uncompressed-length">
         <code class="literal">
          UNCOMPRESSED_LENGTH()
         </code>
        </a>
       </td>
       <td>
        Return the length of a string before compression
       </td>
      </tr>
      <tr>
       <td>
        <a class="link" href="encryption-functions.html#function_validate-password-strength">
         <code class="literal">
          VALIDATE_PASSWORD_STRENGTH()
         </code>
        </a>
       </td>
       <td>
        Determine strength of password
       </td>
      </tr>
     </tbody>
    </table>
   </div>
   <div class="table-contents">
    <table cellpadding="0" cellspacing="0" style="position: fixed; top: 0px; display: none; left: 401px; width: 739px;">
     <thead>
      <tr>
       <th style="width: 234.516px;">
        Name
       </th>
       <th style="width: 503.484px;">
        Description
       </th>
      </tr>
     </thead>
    </table>
   </div>
  </div>
  <br class="table-break"/>
  <p>
   Many encryption and compression functions return strings for which
      the result might contain arbitrary byte values. If you want to
      store these results, use a column with a
   <a class="link" href="binary-varbinary.html" title="13.3.3 The BINARY and VARBINARY Types">
    <code class="literal">
     VARBINARY
    </code>
   </a>
   or
   <a class="link" href="blob.html" title="13.3.4 The BLOB and TEXT Types">
    <code class="literal">
     BLOB
    </code>
   </a>
   binary string data type. This
      avoids potential problems with trailing space removal or character
      set conversion that would change data values, such as may occur if
      you use a nonbinary string data type
      (
   <a class="link" href="char.html" title="13.3.2 The CHAR and VARCHAR Types">
    <code class="literal">
     CHAR
    </code>
   </a>
   ,
   <a class="link" href="char.html" title="13.3.2 The CHAR and VARCHAR Types">
    <code class="literal">
     VARCHAR
    </code>
   </a>
   ,
   <a class="link" href="blob.html" title="13.3.4 The BLOB and TEXT Types">
    <code class="literal">
     TEXT
    </code>
   </a>
   ).
  </p>
  <p>
   Some encryption functions return strings of ASCII characters:
   <a class="link" href="encryption-functions.html#function_md5">
    <code class="literal">
     MD5()
    </code>
   </a>
   ,
   <a class="link" href="encryption-functions.html#function_sha1">
    <code class="literal">
     SHA()
    </code>
   </a>
   ,
   <a class="link" href="encryption-functions.html#function_sha1">
    <code class="literal">
     SHA1()
    </code>
   </a>
   ,
   <a class="link" href="encryption-functions.html#function_sha2">
    <code class="literal">
     SHA2()
    </code>
   </a>
   ,
   <a class="link" href="encryption-functions.html#function_statement-digest">
    <code class="literal">
     STATEMENT_DIGEST()
    </code>
   </a>
   ,
   <a class="link" href="encryption-functions.html#function_statement-digest-text">
    <code class="literal">
     STATEMENT_DIGEST_TEXT()
    </code>
   </a>
   . Their
      return value is a string that has a character set and collation
      determined by the
   <a class="link" href="server-system-variables.html#sysvar_character_set_connection">
    <code class="literal">
     character_set_connection
    </code>
   </a>
   and
   <a class="link" href="server-system-variables.html#sysvar_collation_connection">
    <code class="literal">
     collation_connection
    </code>
   </a>
   system
      variables. This is a nonbinary string unless the character set is
   <code class="literal">
    binary
   </code>
   .
  </p>
  <p>
   If an application stores values from a function such as
   <a class="link" href="encryption-functions.html#function_md5">
    <code class="literal">
     MD5()
    </code>
   </a>
   or
   <a class="link" href="encryption-functions.html#function_sha1">
    <code class="literal">
     SHA1()
    </code>
   </a>
   that returns a string of hex
      digits, more efficient storage and comparisons can be obtained by
      converting the hex representation to binary using
   <a class="link" href="string-functions.html#function_unhex">
    <code class="literal">
     UNHEX()
    </code>
   </a>
   and storing the result in a
   <a class="link" href="binary-varbinary.html" title="13.3.3 The BINARY and VARBINARY Types">
    <code class="literal">
     BINARY(
     <em class="replaceable">
      <code>
       N
      </code>
     </em>
     )
    </code>
   </a>
   column. Each pair of hexadecimal digits requires one byte in
      binary form, so the value of
   <em class="replaceable">
    <code>
     N
    </code>
   </em>
   depends
      on the length of the hex string.
   <em class="replaceable">
    <code>
     N
    </code>
   </em>
   is
      16 for an
   <a class="link" href="encryption-functions.html#function_md5">
    <code class="literal">
     MD5()
    </code>
   </a>
   value and 20 for a
   <a class="link" href="encryption-functions.html#function_sha1">
    <code class="literal">
     SHA1()
    </code>
   </a>
   value. For
   <a class="link" href="encryption-functions.html#function_sha2">
    <code class="literal">
     SHA2()
    </code>
   </a>
   ,
   <em class="replaceable">
    <code>
     N
    </code>
   </em>
   ranges from 28 to 32 depending on the
      argument specifying the desired bit length of the result.
  </p>
  <p>
   The size penalty for storing the hex string in a
   <a class="link" href="char.html" title="13.3.2 The CHAR and VARCHAR Types">
    <code class="literal">
     CHAR
    </code>
   </a>
   column is at least two times,
      up to eight times if the value is stored in a column that uses the
   <code class="literal">
    utf8mb4
   </code>
   character set (where each character
      uses 4 bytes). Storing the string also results in slower
      comparisons because of the larger values and the need to take
      character set collation rules into account.
  </p>
  <p>
   Suppose that an application stores
   <a class="link" href="encryption-functions.html#function_md5">
    <code class="literal">
     MD5()
    </code>
   </a>
   string values in a
   <a class="link" href="char.html" title="13.3.2 The CHAR and VARCHAR Types">
    <code class="literal">
     CHAR(32)
    </code>
   </a>
   column:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa25439234"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> md5_tbl <span class="token punctuation">(</span>md5_val <span class="token datatype">CHAR</span><span class="token punctuation">(</span><span class="token number">32</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> md5_tbl <span class="token punctuation">(</span>md5_val<span class="token punctuation">,</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span> <span class="token keyword">VALUES</span><span class="token punctuation">(</span><span class="token function">MD5</span><span class="token punctuation">(</span><span class="token string">'abcdef'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
  </div>
  <p>
   To convert hex strings to more compact form, modify the
      application to use
   <a class="link" href="string-functions.html#function_unhex">
    <code class="literal">
     UNHEX()
    </code>
   </a>
   and
   <a class="link" href="binary-varbinary.html" title="13.3.3 The BINARY and VARBINARY Types">
    <code class="literal">
     BINARY(16)
    </code>
   </a>
   instead as follows:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa28295470"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> md5_tbl <span class="token punctuation">(</span>md5_val <span class="token datatype">BINARY</span><span class="token punctuation">(</span><span class="token number">16</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> md5_tbl <span class="token punctuation">(</span>md5_val<span class="token punctuation">,</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span> <span class="token keyword">VALUES</span><span class="token punctuation">(</span><span class="token function">UNHEX</span><span class="token punctuation">(</span><span class="token function">MD5</span><span class="token punctuation">(</span><span class="token string">'abcdef'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
  </div>
  <p>
   Applications should be prepared to handle the very rare case that
      a hashing function produces the same value for two different input
      values. One way to make collisions detectable is to make the hash
      column a primary key.
  </p>
  <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
   <div class="admon-title">
    Note
   </div>
   <p>
    Exploits for the MD5 and SHA-1 algorithms have become known. You
        may wish to consider using another one-way encryption function
        described in this section instead, such as
    <a class="link" href="encryption-functions.html#function_sha2">
     <code class="literal">
      SHA2()
     </code>
    </a>
    .
   </p>
  </div>
  <div class="caution" style="margin-left: 0.5in; margin-right: 0.5in;">
   <div class="admon-title">
    Caution
   </div>
   <p>
    Passwords or other sensitive values supplied as arguments to
        encryption functions are sent as cleartext to the MySQL server
        unless an SSL connection is used. Also, such values appear in
        any MySQL logs to which they are written. To avoid these types
        of exposure, applications can encrypt sensitive values on the
        client side before sending them to the server. The same
        considerations apply to encryption keys. To avoid exposing
        these, applications can use stored procedures to encrypt and
        decrypt values on the server side.
   </p>
  </div>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <a name="function_aes-decrypt">
     </a>
     <p>
      <a class="link" href="encryption-functions.html#function_aes-decrypt">
       <code class="literal">
        AES_DECRYPT(
        <em class="replaceable">
         <code>
          crypt_str
         </code>
        </em>
        ,
        <em class="replaceable">
         <code>
          key_str
         </code>
        </em>
        [,
        <em class="replaceable">
         <code>
          init_vector
         </code>
        </em>
        ][,
        <em class="replaceable">
         <code>
          kdf_name
         </code>
        </em>
        ][,
        <em class="replaceable">
         <code>
          salt
         </code>
        </em>
        ][,
        <em class="replaceable">
         <code>
          info
          | iterations
         </code>
        </em>
        ])
       </code>
      </a>
     </p>
     <a class="indexterm" name="idm46045199517136">
     </a>
     <p>
      This function decrypts data using the official AES (Advanced
          Encryption Standard) algorithm. For more information, see the
          description of
      <a class="link" href="encryption-functions.html#function_aes-encrypt">
       <code class="literal">
        AES_ENCRYPT()
       </code>
      </a>
      .
     </p>
     <p>
      Statements that use
      <a class="link" href="encryption-functions.html#function_aes-decrypt">
       <code class="literal">
        AES_DECRYPT()
       </code>
      </a>
      are unsafe for
          statement-based replication.
     </p>
    </li>
    <li class="listitem">
     <a name="function_aes-encrypt">
     </a>
     <p>
      <a class="link" href="encryption-functions.html#function_aes-encrypt">
       <code class="literal">
        AES_ENCRYPT(
        <em class="replaceable">
         <code>
          str
         </code>
        </em>
        ,
        <em class="replaceable">
         <code>
          key_str
         </code>
        </em>
        [,
        <em class="replaceable">
         <code>
          init_vector
         </code>
        </em>
        ][,
        <em class="replaceable">
         <code>
          kdf_name
         </code>
        </em>
        ][,
        <em class="replaceable">
         <code>
          salt
         </code>
        </em>
        ][,
        <em class="replaceable">
         <code>
          info
          | iterations
         </code>
        </em>
        ])
       </code>
      </a>
     </p>
     <a class="indexterm" name="idm46045199504032">
     </a>
     <p>
      <a class="link" href="encryption-functions.html#function_aes-encrypt">
       <code class="literal">
        AES_ENCRYPT()
       </code>
      </a>
      and
      <a class="link" href="encryption-functions.html#function_aes-decrypt">
       <code class="literal">
        AES_DECRYPT()
       </code>
      </a>
      implement
          encryption and decryption of data using the official AES
          (Advanced Encryption Standard) algorithm, previously known as
      <span class="quote">
       “
       <span class="quote">
        Rijndael.
       </span>
       ”
      </span>
      The AES standard permits various key
          lengths. By default these functions implement AES with a
          128-bit key length. Key lengths of 196 or 256 bits can be
          used, as described later. The key length is a trade off
          between performance and security.
     </p>
     <p>
      <a class="link" href="encryption-functions.html#function_aes-encrypt">
       <code class="literal">
        AES_ENCRYPT()
       </code>
      </a>
      encrypts the
          string
      <em class="replaceable">
       <code>
        str
       </code>
      </em>
      using the key string
      <em class="replaceable">
       <code>
        key_str
       </code>
      </em>
      , and returns a binary
          string containing the encrypted output.
      <a class="link" href="encryption-functions.html#function_aes-decrypt">
       <code class="literal">
        AES_DECRYPT()
       </code>
      </a>
      decrypts the
          encrypted string
      <em class="replaceable">
       <code>
        crypt_str
       </code>
      </em>
      using
          the key string
      <em class="replaceable">
       <code>
        key_str
       </code>
      </em>
      , and returns
          the original (binary) string in hexadecimal format. (To obtain
          the string as plaintext, cast the result to
      <code class="literal">
       CHAR
      </code>
      . Alternatively, start the
      <a class="link" href="mysql.html" title="6.5.1 mysql — The MySQL Command-Line Client">
       <span class="command">
        <strong>
         mysql
        </strong>
       </span>
      </a>
      client with
      <a class="link" href="mysql-command-options.html#option_mysql_binary-as-hex">
       <code class="option">
        --skip-binary-as-hex
       </code>
      </a>
      to cause all binary values to be displayed as text.) If either
          function argument is
      <code class="literal">
       NULL
      </code>
      , the function
          returns
      <code class="literal">
       NULL
      </code>
      . If
      <a class="link" href="encryption-functions.html#function_aes-decrypt">
       <code class="literal">
        AES_DECRYPT()
       </code>
      </a>
      detects invalid
          data or incorrect padding, it returns
      <code class="literal">
       NULL
      </code>
      .
          However, it is possible for
      <a class="link" href="encryption-functions.html#function_aes-decrypt">
       <code class="literal">
        AES_DECRYPT()
       </code>
      </a>
      to return a
          non-
      <code class="literal">
       NULL
      </code>
      value (possibly garbage) if the
          input data or the key is invalid.
     </p>
     <p>
      These functions support the use of a key derivation function
          (KDF) to create a cryptographically strong secret key from the
          information passed in
      <em class="replaceable">
       <code>
        key_str
       </code>
      </em>
      . The
          derived key is used to encrypt and decrypt the data, and it
          remains in the MySQL Server instance and is not accessible to
          users. Using a KDF is highly recommended, as it provides
          better security than specifying your own premade key or
          deriving it by a simpler method as you use the function. The
          functions support HKDF (available from OpenSSL 1.1.0), for
          which you can specify an optional salt and context-specific
          information to include in the keying material, and PBKDF2
          (available from OpenSSL 1.0.2), for which you can specify an
          optional salt and set the number of iterations used to produce
          the key.
     </p>
     <p>
      <a class="link" href="encryption-functions.html#function_aes-encrypt">
       <code class="literal">
        AES_ENCRYPT()
       </code>
      </a>
      and
      <a class="link" href="encryption-functions.html#function_aes-decrypt">
       <code class="literal">
        AES_DECRYPT()
       </code>
      </a>
      permit control of
          the block encryption mode. The
      <a class="link" href="server-system-variables.html#sysvar_block_encryption_mode">
       <code class="literal">
        block_encryption_mode
       </code>
      </a>
      system
          variable controls the mode for block-based encryption
          algorithms. Its default value is
      <code class="literal">
       aes-128-ecb
      </code>
      , which signifies encryption
          using a key length of 128 bits and ECB mode. For a description
          of the permitted values of this variable, see
      <a class="xref" href="server-system-variables.html" title="7.1.8 Server System Variables">
       Section 7.1.8, “Server System Variables”
      </a>
      . The optional
      <em class="replaceable">
       <code>
        init_vector
       </code>
      </em>
      argument is used to
          provide an initialization vector for block encryption modes
          that require it.
     </p>
     <p>
      Statements that use
      <a class="link" href="encryption-functions.html#function_aes-encrypt">
       <code class="literal">
        AES_ENCRYPT()
       </code>
      </a>
      or
      <a class="link" href="encryption-functions.html#function_aes-decrypt">
       <code class="literal">
        AES_DECRYPT()
       </code>
      </a>
      are unsafe for
          statement-based replication.
     </p>
     <p>
      If
      <a class="link" href="encryption-functions.html#function_aes-encrypt">
       <code class="literal">
        AES_ENCRYPT()
       </code>
      </a>
      is invoked
          from within the
      <a class="link" href="mysql.html" title="6.5.1 mysql — The MySQL Command-Line Client">
       <span class="command">
        <strong>
         mysql
        </strong>
       </span>
      </a>
      client, binary
          strings display using hexadecimal notation, depending on the
          value of the
      <a class="link" href="mysql-command-options.html#option_mysql_binary-as-hex">
       <code class="option">
        --binary-as-hex
       </code>
      </a>
      .
          For more information about that option, see
      <a class="xref" href="mysql.html" title="6.5.1 mysql — The MySQL Command-Line Client">
       Section 6.5.1, “mysql — The MySQL Command-Line Client”
      </a>
      .
     </p>
     <p>
      The arguments for the
      <a class="link" href="encryption-functions.html#function_aes-encrypt">
       <code class="literal">
        AES_ENCRYPT()
       </code>
      </a>
      and
      <a class="link" href="encryption-functions.html#function_aes-decrypt">
       <code class="literal">
        AES_DECRYPT()
       </code>
      </a>
      functions are as
          follows:
     </p>
     <div class="variablelist">
      <dl class="variablelist">
       <dt>
        <span class="term">
         <em class="replaceable">
          <code>
           str
          </code>
         </em>
        </span>
       </dt>
       <dd>
        <p>
         The string for
         <a class="link" href="encryption-functions.html#function_aes-encrypt">
          <code class="literal">
           AES_ENCRYPT()
          </code>
         </a>
         to encrypt
                using the key string
         <em class="replaceable">
          <code>
           key_str
          </code>
         </em>
         ,
                or the key derived from it by the specified KDF. The
                string can be any length. Padding is automatically added
                to
         <em class="replaceable">
          <code>
           str
          </code>
         </em>
         so it is a multiple of
                a block as required by block-based algorithms such as
                AES. This padding is automatically removed by the
         <a class="link" href="encryption-functions.html#function_aes-decrypt">
          <code class="literal">
           AES_DECRYPT()
          </code>
         </a>
         function.
        </p>
       </dd>
       <dt>
        <span class="term">
         <em class="replaceable">
          <code>
           crypt_str
          </code>
         </em>
        </span>
       </dt>
       <dd>
        <p>
         The encrypted string for
         <a class="link" href="encryption-functions.html#function_aes-decrypt">
          <code class="literal">
           AES_DECRYPT()
          </code>
         </a>
         to decrypt
                using the key string
         <em class="replaceable">
          <code>
           key_str
          </code>
         </em>
         ,
                or the key derived from it by the specified KDF. The
                string can be any length. The length of
         <em class="replaceable">
          <code>
           crypt_str
          </code>
         </em>
         can be calculated
                from the length of the original string using this
                formula:
        </p>
        <div class="copytoclipboard-wrapper">
         <pre class="programlisting one-line language-clike"><div class="docs-select-all right" id="sa73279735"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-clike"><span class="token number">16</span> <span class="token operator">*</span> <span class="token punctuation">(</span><span class="token function">trunc</span><span class="token punctuation">(</span><em class="replaceable">string_length</em> <span class="token operator">/</span> <span class="token number">16</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span></code></pre>
        </div>
       </dd>
       <dt>
        <span class="term">
         <em class="replaceable">
          <code>
           key_str
          </code>
         </em>
        </span>
       </dt>
       <dd>
        <p>
         The encryption key, or the input keying material that is
                used as the basis for deriving a key using a key
                derivation function (KDF). For the same instance of
                data, use the same value of
         <em class="replaceable">
          <code>
           key_str
          </code>
         </em>
         for encryption with
         <a class="link" href="encryption-functions.html#function_aes-encrypt">
          <code class="literal">
           AES_ENCRYPT()
          </code>
         </a>
         and
                decryption with
         <a class="link" href="encryption-functions.html#function_aes-decrypt">
          <code class="literal">
           AES_DECRYPT()
          </code>
         </a>
         .
        </p>
        <p>
         If you are using a KDF,
         <em class="replaceable">
          <code>
           key_str
          </code>
         </em>
         can be any arbitrary
                information such as a password or passphrase. In the
                further arguments for the function, you specify the KDF
                name, then add further options to increase the security
                as appropriate for the KDF.
        </p>
        <p>
         When you use a KDF, the function creates a
                cryptographically strong secret key from the information
                passed in
         <em class="replaceable">
          <code>
           key_str
          </code>
         </em>
         and any
                salt or additional information that you provide in the
                other arguments. The derived key is used to encrypt and
                decrypt the data, and it remains in the MySQL Server
                instance and is not accessible to users. Using a KDF is
                highly recommended, as it provides better security than
                specifying your own premade key or deriving it by a
                simpler method as you use the function.
        </p>
        <p>
         If you are not using a KDF, for a key length of 128
                bits, the most secure way to pass a key to the
         <em class="replaceable">
          <code>
           key_str
          </code>
         </em>
         argument is to create
                a truly random 128-bit value and pass it as a binary
                value. For example:
        </p>
        <div class="copytoclipboard-wrapper">
         <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa11696982"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> t
<span class="token keyword">VALUES</span> <span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token function">AES_ENCRYPT</span><span class="token punctuation">(</span><span class="token string">'text'</span><span class="token punctuation">,</span><span class="token function">UNHEX</span><span class="token punctuation">(</span><span class="token string">'F3229A0B371ED2D9441B830D21A390C3'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
        </div>
        <p>
         A passphrase can be used to generate an AES key by
                hashing the passphrase. For example:
        </p>
        <div class="copytoclipboard-wrapper">
         <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa34738136"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> t
<span class="token keyword">VALUES</span> <span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token function">AES_ENCRYPT</span><span class="token punctuation">(</span><span class="token string">'text'</span><span class="token punctuation">,</span> <span class="token function">UNHEX</span><span class="token punctuation">(</span><span class="token function">SHA2</span><span class="token punctuation">(</span><span class="token string">'My secret passphrase'</span><span class="token punctuation">,</span><span class="token number">512</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
        </div>
        <p>
         If you exceed the maximum key length of 128 bits, a
                warning is returned. If you are not using a KDF, do not
                pass a password or passphrase directly to
         <em class="replaceable">
          <code>
           key_str
          </code>
         </em>
         , hash it first.
                Previous versions of this documentation suggested the
                former approach, but it is no longer recommended as the
                examples shown here are more secure.
        </p>
       </dd>
       <dt>
        <span class="term">
         <em class="replaceable">
          <code>
           init_vector
          </code>
         </em>
        </span>
       </dt>
       <dd>
        <p>
         An initialization vector, for block encryption modes
                that require it. The
         <a class="link" href="server-system-variables.html#sysvar_block_encryption_mode">
          <code class="literal">
           block_encryption_mode
          </code>
         </a>
         system variable controls the mode. For the same instance
                of data, use the same value of
         <em class="replaceable">
          <code>
           init_vector
          </code>
         </em>
         for encryption
                with
         <a class="link" href="encryption-functions.html#function_aes-encrypt">
          <code class="literal">
           AES_ENCRYPT()
          </code>
         </a>
         and
                decryption with
         <a class="link" href="encryption-functions.html#function_aes-decrypt">
          <code class="literal">
           AES_DECRYPT()
          </code>
         </a>
         .
        </p>
        <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
         <div class="admon-title">
          Note
         </div>
         <p>
          If you are using a KDF, you must specify an
                  initialization vector or a null string for this
                  argument, in order to access the later arguments to
                  define the KDF.
         </p>
        </div>
        <p>
         For modes that require an initialization vector, it must
                be 16 bytes or longer (bytes in excess of 16 are
                ignored). An error occurs if
         <em class="replaceable">
          <code>
           init_vector
          </code>
         </em>
         is missing. For
                modes that do not require an initialization vector, it
                is ignored and a warning is generated if
         <em class="replaceable">
          <code>
           init_vector
          </code>
         </em>
         is specified,
                unless you are using a KDF.
        </p>
        <p>
         The default value for the
         <a class="link" href="server-system-variables.html#sysvar_block_encryption_mode">
          <code class="literal">
           block_encryption_mode
          </code>
         </a>
         system variable is
         <code class="literal">
          aes-128-ecb
         </code>
         , or
                ECB mode, which does not require an initialization
                vector. The alternative permitted block encryption modes
                CBC, CFB1, CFB8, CFB128, and OFB all require an
                initialization vector.
        </p>
        <p>
         A random string of bytes to use for the initialization
                vector can be produced by calling
         <a class="link" href="encryption-functions.html#function_random-bytes">
          <code class="literal">
           RANDOM_BYTES(16)
          </code>
         </a>
         .
        </p>
       </dd>
       <dt>
        <span class="term">
         <em class="replaceable">
          <code>
           kdf_name
          </code>
         </em>
        </span>
       </dt>
       <dd>
        <p>
         The name of the key derivation function (KDF) to create
                a key from the input keying material passed in
         <em class="replaceable">
          <code>
           key_str
          </code>
         </em>
         , and other arguments
                as appropriate for the KDF. Optional.
        </p>
        <p>
         For the same instance of data, use the same value of
         <em class="replaceable">
          <code>
           kdf_name
          </code>
         </em>
         for encryption with
         <a class="link" href="encryption-functions.html#function_aes-encrypt">
          <code class="literal">
           AES_ENCRYPT()
          </code>
         </a>
         and
                decryption with
         <a class="link" href="encryption-functions.html#function_aes-decrypt">
          <code class="literal">
           AES_DECRYPT()
          </code>
         </a>
         . When you
                specify
         <em class="replaceable">
          <code>
           kdf_name
          </code>
         </em>
         , you must
                specify
         <em class="replaceable">
          <code>
           init_vector
          </code>
         </em>
         , using
                either a valid initialization vector, or a null string
                if the encryption mode does not require an
                initialization vector.
        </p>
        <p>
         The following values are supported:
        </p>
        <div class="variablelist">
         <dl class="variablelist">
          <dt>
           <span class="term">
            <code class="literal">
             hkdf
            </code>
           </span>
          </dt>
          <dd>
           <p>
            HKDF, which is available from OpenSSL 1.1.0. HKDF
                      extracts a pseudorandom key from the keying
                      material then expands it into additional keys.
                      With HKDF, you can specify an optional salt
                      (
            <em class="replaceable">
             <code>
              salt
             </code>
            </em>
            ) and
                      context-specific information such as application
                      details (
            <em class="replaceable">
             <code>
              info
             </code>
            </em>
            ) to
                      include in the keying material.
           </p>
          </dd>
          <dt>
           <span class="term">
            <code class="literal">
             pbkdf2_hmac
            </code>
           </span>
          </dt>
          <dd>
           <p>
            PBKDF2, which is available from OpenSSL 1.0.2.
                      PBKDF2 applies a pseudorandom function to the
                      keying material, and repeats this process a large
                      number of times to produce the key. With PBKDF2,
                      you can specify an optional salt
                      (
            <em class="replaceable">
             <code>
              salt
             </code>
            </em>
            ) to include in
                      the keying material, and set the number of
                      iterations used to produce the key
                      (
            <em class="replaceable">
             <code>
              iterations
             </code>
            </em>
            ).
           </p>
          </dd>
         </dl>
        </div>
        <p>
         In this example, HKDF is specified as the key derivation
                function, and a salt and context information are
                provided. The argument for the initialization vector is
                included but is the empty string:
        </p>
        <div class="copytoclipboard-wrapper">
         <pre class="programlisting one-line language-sql"><div class="docs-select-all right" id="sa2697150"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">SELECT</span> <span class="token function">AES_ENCRYPT</span><span class="token punctuation">(</span><span class="token string">'mytext'</span><span class="token punctuation">,</span><span class="token string">'mykeystring'</span><span class="token punctuation">,</span> <span class="token string">''</span><span class="token punctuation">,</span> <span class="token string">'hkdf'</span><span class="token punctuation">,</span> <span class="token string">'salt'</span><span class="token punctuation">,</span> <span class="token string">'info'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
        </div>
        <p>
         In this example, PBKDF2 is specified as the key
                derivation function, a salt is provided, and the number
                of iterations is doubled from the recommended minimum:
        </p>
        <div class="copytoclipboard-wrapper">
         <pre class="programlisting one-line language-sql"><div class="docs-select-all right" id="sa13169593"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">SELECT</span> <span class="token function">AES_ENCRYPT</span><span class="token punctuation">(</span><span class="token string">'mytext'</span><span class="token punctuation">,</span><span class="token string">'mykeystring'</span><span class="token punctuation">,</span> <span class="token string">''</span><span class="token punctuation">,</span> <span class="token string">'pbkdf2_hmac'</span><span class="token punctuation">,</span><span class="token string">'salt'</span><span class="token punctuation">,</span> <span class="token string">'2000'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
        </div>
       </dd>
       <dt>
        <span class="term">
         <em class="replaceable">
          <code>
           salt
          </code>
         </em>
        </span>
       </dt>
       <dd>
        <p>
         A salt to be passed to the key derivation function
                (KDF). Optional. Both HKDF and PBKDF2 can use salts, and
                their use is recommended to help prevent attacks based
                on dictionaries of common passwords or rainbow tables.
        </p>
        <p>
         A salt consists of random data, which for security must
                be different for each encryption operation. A random
                string of bytes to use for the salt can be produced by
                calling
         <a class="link" href="encryption-functions.html#function_random-bytes">
          <code class="literal">
           RANDOM_BYTES()
          </code>
         </a>
         .
                This example produces a 64-bit salt:
        </p>
        <div class="copytoclipboard-wrapper">
         <pre class="programlisting one-line language-sql"><div class="docs-select-all right" id="sa69780846"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">SET</span> <span class="token variable">@salt</span> <span class="token operator">=</span> <span class="token function">RANDOM_BYTES</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
        </div>
        <p>
         For the same instance of data, use the same value of
         <em class="replaceable">
          <code>
           salt
          </code>
         </em>
         for encryption with
         <a class="link" href="encryption-functions.html#function_aes-encrypt">
          <code class="literal">
           AES_ENCRYPT()
          </code>
         </a>
         and
                decryption with
         <a class="link" href="encryption-functions.html#function_aes-decrypt">
          <code class="literal">
           AES_DECRYPT()
          </code>
         </a>
         . The salt
                can safely be stored along with the encrypted data.
        </p>
       </dd>
       <dt>
        <span class="term">
         <em class="replaceable">
          <code>
           info
          </code>
         </em>
        </span>
       </dt>
       <dd>
        <p>
         Context-specific information for HKDF to include in the
                keying material, such as information about the
                application. Optional; available when you specify
         <code class="literal">
          hkdf
         </code>
         as the KDF name. HKDF adds this
                information to the keying material specified in
         <em class="replaceable">
          <code>
           key_str
          </code>
         </em>
         and the salt
                specified in
         <em class="replaceable">
          <code>
           salt
          </code>
         </em>
         to produce
                the key.
        </p>
        <p>
         For the same instance of data, use the same value of
         <em class="replaceable">
          <code>
           info
          </code>
         </em>
         for encryption with
         <a class="link" href="encryption-functions.html#function_aes-encrypt">
          <code class="literal">
           AES_ENCRYPT()
          </code>
         </a>
         and
                decryption with
         <a class="link" href="encryption-functions.html#function_aes-decrypt">
          <code class="literal">
           AES_DECRYPT()
          </code>
         </a>
         .
        </p>
       </dd>
       <dt>
        <span class="term">
         <em class="replaceable">
          <code>
           iterations
          </code>
         </em>
        </span>
       </dt>
       <dd>
        <p>
         The iteration count for PBKDF2 to use when producing the
                key. Optional; available when you specify
         <code class="literal">
          pbkdf2_hmac
         </code>
         as the KDF name. A higher
                count gives greater resistance to brute-force attacks
                because it has a greater computational cost for the
                attacker, but the same is necessarily true for the key
                derivation process. The default if you do not specify
                this argument is 1000, which is the minimum recommended
                by the OpenSSL standard.
        </p>
        <p>
         For the same instance of data, use the same value of
         <em class="replaceable">
          <code>
           iterations
          </code>
         </em>
         for encryption
                with
         <a class="link" href="encryption-functions.html#function_aes-encrypt">
          <code class="literal">
           AES_ENCRYPT()
          </code>
         </a>
         and
                decryption with
         <a class="link" href="encryption-functions.html#function_aes-decrypt">
          <code class="literal">
           AES_DECRYPT()
          </code>
         </a>
         .
        </p>
       </dd>
      </dl>
     </div>
     <p>
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa96736067"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token prompt">mysql&gt;</span> <span class="token keyword">SET</span> block_encryption_mode <span class="token operator">=</span> <span class="token string">'aes-256-cbc'</span><span class="token punctuation">;</span>
<span class="token prompt">mysql&gt;</span> <span class="token keyword">SET</span> <span class="token variable">@key_str</span> <span class="token operator">=</span> <span class="token function">SHA2</span><span class="token punctuation">(</span><span class="token string">'My secret passphrase'</span><span class="token punctuation">,</span><span class="token number">512</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token prompt">mysql&gt;</span> <span class="token keyword">SET</span> <span class="token variable">@init_vector</span> <span class="token operator">=</span> <span class="token function">RANDOM_BYTES</span><span class="token punctuation">(</span><span class="token number">16</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token prompt">mysql&gt;</span> <span class="token keyword">SET</span> <span class="token variable">@crypt_str</span> <span class="token operator">=</span> <span class="token function">AES_ENCRYPT</span><span class="token punctuation">(</span><span class="token string">'text'</span><span class="token punctuation">,</span><span class="token variable">@key_str</span><span class="token punctuation">,</span><span class="token variable">@init_vector</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token function">CAST</span><span class="token punctuation">(</span><span class="token function">AES_DECRYPT</span><span class="token punctuation">(</span><span class="token variable">@crypt_str</span><span class="token punctuation">,</span><span class="token variable">@key_str</span><span class="token punctuation">,</span><span class="token variable">@init_vector</span><span class="token punctuation">)</span> <span class="token keyword">AS</span> <span class="token datatype">CHAR</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> CAST(AES_DECRYPT(@crypt_str,@key_str,@init_vector) AS CHAR) <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> text                                                        <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span></code></pre>
     </div>
    </li>
    <li class="listitem">
     <a name="function_compress">
     </a>
     <p>
      <a class="link" href="encryption-functions.html#function_compress">
       <code class="literal">
        COMPRESS(
        <em class="replaceable">
         <code>
          string_to_compress
         </code>
        </em>
        )
       </code>
      </a>
     </p>
     <a class="indexterm" name="idm46045199375152">
     </a>
     <p>
      Compresses a string and returns the result as a binary string.
          This function requires MySQL to have been compiled with a
          compression library such as
      <code class="literal">
       zlib
      </code>
      .
          Otherwise, the return value is always
      <code class="literal">
       NULL
      </code>
      .
          The return value is also
      <code class="literal">
       NULL
      </code>
      if
      <em class="replaceable">
       <code>
        string_to_compress
       </code>
      </em>
      is
      <code class="literal">
       NULL
      </code>
      . The compressed string can be
          uncompressed with
      <a class="link" href="encryption-functions.html#function_uncompress">
       <code class="literal">
        UNCOMPRESS()
       </code>
      </a>
      .
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa47687868"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token function">LENGTH</span><span class="token punctuation">(</span><span class="token function">COMPRESS</span><span class="token punctuation">(</span><span class="token function">REPEAT</span><span class="token punctuation">(</span><span class="token string">'a'</span><span class="token punctuation">,</span><span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token number">21</span>
<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token function">LENGTH</span><span class="token punctuation">(</span><span class="token function">COMPRESS</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token number">0</span>
<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token function">LENGTH</span><span class="token punctuation">(</span><span class="token function">COMPRESS</span><span class="token punctuation">(</span><span class="token string">'a'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token number">13</span>
<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token function">LENGTH</span><span class="token punctuation">(</span><span class="token function">COMPRESS</span><span class="token punctuation">(</span><span class="token function">REPEAT</span><span class="token punctuation">(</span><span class="token string">'a'</span><span class="token punctuation">,</span><span class="token number">16</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token number">15</span></code></pre>
     </div>
     <p>
      The compressed string contents are stored the following way:
     </p>
     <div class="itemizedlist">
      <ul class="itemizedlist" style="list-style-type: circle; ">
       <li class="listitem">
        <p>
         Empty strings are stored as empty strings.
        </p>
       </li>
       <li class="listitem">
        <p>
         Nonempty strings are stored as a 4-byte length of the
              uncompressed string (low byte first), followed by the
              compressed string. If the string ends with space, an extra
         <code class="literal">
          .
         </code>
         character is added to avoid problems
              with endspace trimming should the result be stored in a
         <a class="link" href="char.html" title="13.3.2 The CHAR and VARCHAR Types">
          <code class="literal">
           CHAR
          </code>
         </a>
         or
         <a class="link" href="char.html" title="13.3.2 The CHAR and VARCHAR Types">
          <code class="literal">
           VARCHAR
          </code>
         </a>
         column. (However,
              use of nonbinary string data types such as
         <a class="link" href="char.html" title="13.3.2 The CHAR and VARCHAR Types">
          <code class="literal">
           CHAR
          </code>
         </a>
         or
         <a class="link" href="char.html" title="13.3.2 The CHAR and VARCHAR Types">
          <code class="literal">
           VARCHAR
          </code>
         </a>
         to store compressed
              strings is not recommended anyway because character set
              conversion may occur. Use a
         <a class="link" href="binary-varbinary.html" title="13.3.3 The BINARY and VARBINARY Types">
          <code class="literal">
           VARBINARY
          </code>
         </a>
         or
         <a class="link" href="blob.html" title="13.3.4 The BLOB and TEXT Types">
          <code class="literal">
           BLOB
          </code>
         </a>
         binary string column
              instead.)
        </p>
       </li>
      </ul>
     </div>
     <p>
      If
      <a class="link" href="encryption-functions.html#function_compress">
       <code class="literal">
        COMPRESS()
       </code>
      </a>
      is invoked from
          within the
      <a class="link" href="mysql.html" title="6.5.1 mysql — The MySQL Command-Line Client">
       <span class="command">
        <strong>
         mysql
        </strong>
       </span>
      </a>
      client, binary strings
          display using hexadecimal notation, depending on the value of
          the
      <a class="link" href="mysql-command-options.html#option_mysql_binary-as-hex">
       <code class="option">
        --binary-as-hex
       </code>
      </a>
      . For more
          information about that option, see
      <a class="xref" href="mysql.html" title="6.5.1 mysql — The MySQL Command-Line Client">
       Section 6.5.1, “mysql — The MySQL Command-Line Client”
      </a>
      .
     </p>
    </li>
    <li class="listitem">
     <a name="function_md5">
     </a>
     <p>
      <a class="link" href="encryption-functions.html#function_md5">
       <code class="literal">
        MD5(
        <em class="replaceable">
         <code>
          str
         </code>
        </em>
        )
       </code>
      </a>
     </p>
     <a class="indexterm" name="idm46045199342144">
     </a>
     <p>
      Calculates an MD5 128-bit checksum for the string. The value
          is returned as a string of 32 hexadecimal digits, or
      <code class="literal">
       NULL
      </code>
      if the argument was
      <code class="literal">
       NULL
      </code>
      . The return value can, for example, be
          used as a hash key. See the notes at the beginning of this
          section about storing hash values efficiently.
     </p>
     <p>
      The return value is a string in the connection character set.
     </p>
     <p>
      If FIPS mode is enabled,
      <code class="function">
       MD5()
      </code>
      returns
      <code class="literal">
       NULL
      </code>
      . See
      <a class="xref" href="fips-mode.html" title="8.8 FIPS Support">
       Section 8.8, “FIPS Support”
      </a>
      .
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa77658121"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token function">MD5</span><span class="token punctuation">(</span><span class="token string">'testing'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token string">'ae2b1fca515949e5d54fb22b8ed95575'</span></code></pre>
     </div>
     <p>
      This is the
      <span class="quote">
       “
       <span class="quote">
        RSA Data Security, Inc. MD5 Message-Digest
          Algorithm.
       </span>
       ”
      </span>
     </p>
     <p>
      See the note regarding the MD5 algorithm at the beginning this
          section.
     </p>
    </li>
    <li class="listitem">
     <a name="function_random-bytes">
     </a>
     <p>
      <a class="link" href="encryption-functions.html#function_random-bytes">
       <code class="literal">
        RANDOM_BYTES(
        <em class="replaceable">
         <code>
          len
         </code>
        </em>
        )
       </code>
      </a>
     </p>
     <a class="indexterm" name="idm46045199325552">
     </a>
     <p>
      This function returns a binary string of
      <em class="replaceable">
       <code>
        len
       </code>
      </em>
      random bytes generated using
          the random number generator of the SSL library. Permitted
          values of
      <em class="replaceable">
       <code>
        len
       </code>
      </em>
      range from 1 to 1024.
          For values outside that range, an error occurs. Returns
      <code class="literal">
       NULL
      </code>
      if
      <em class="replaceable">
       <code>
        len
       </code>
      </em>
      is
      <code class="literal">
       NULL
      </code>
      .
     </p>
     <p>
      <a class="link" href="encryption-functions.html#function_random-bytes">
       <code class="literal">
        RANDOM_BYTES()
       </code>
      </a>
      can be used to
          provide the initialization vector for the
      <a class="link" href="encryption-functions.html#function_aes-decrypt">
       <code class="literal">
        AES_DECRYPT()
       </code>
      </a>
      and
      <a class="link" href="encryption-functions.html#function_aes-encrypt">
       <code class="literal">
        AES_ENCRYPT()
       </code>
      </a>
      functions. For
          use in that context,
      <em class="replaceable">
       <code>
        len
       </code>
      </em>
      must be at
          least 16. Larger values are permitted, but bytes in excess of
          16 are ignored.
     </p>
     <p>
      <a class="link" href="encryption-functions.html#function_random-bytes">
       <code class="literal">
        RANDOM_BYTES()
       </code>
      </a>
      generates a
          random value, which makes its result nondeterministic.
          Consequently, statements that use this function are unsafe for
          statement-based replication.
     </p>
     <p>
      If
      <a class="link" href="encryption-functions.html#function_random-bytes">
       <code class="literal">
        RANDOM_BYTES()
       </code>
      </a>
      is invoked
          from within the
      <a class="link" href="mysql.html" title="6.5.1 mysql — The MySQL Command-Line Client">
       <span class="command">
        <strong>
         mysql
        </strong>
       </span>
      </a>
      client, binary
          strings display using hexadecimal notation, depending on the
          value of the
      <a class="link" href="mysql-command-options.html#option_mysql_binary-as-hex">
       <code class="option">
        --binary-as-hex
       </code>
      </a>
      .
          For more information about that option, see
      <a class="xref" href="mysql.html" title="6.5.1 mysql — The MySQL Command-Line Client">
       Section 6.5.1, “mysql — The MySQL Command-Line Client”
      </a>
      .
     </p>
    </li>
    <li class="listitem">
     <a name="function_sha1">
     </a>
     <p>
      <a class="link" href="encryption-functions.html#function_sha1">
       <code class="literal">
        SHA1(
        <em class="replaceable">
         <code>
          str
         </code>
        </em>
        )
       </code>
      </a>
      ,
      <a class="link" href="encryption-functions.html#function_sha1">
       <code class="literal">
        SHA(
        <em class="replaceable">
         <code>
          str
         </code>
        </em>
        )
       </code>
      </a>
     </p>
     <a class="indexterm" name="idm46045199301952">
     </a>
     <a class="indexterm" name="idm46045199300880">
     </a>
     <p>
      Calculates an SHA-1 160-bit checksum for the string, as
          described in RFC 3174 (Secure Hash Algorithm). The value is
          returned as a string of 40 hexadecimal digits, or
      <code class="literal">
       NULL
      </code>
      if the argument is
      <code class="literal">
       NULL
      </code>
      . One of the possible uses for this
          function is as a hash key. See the notes at the beginning of
          this section about storing hash values efficiently.
      <a class="link" href="encryption-functions.html#function_sha1">
       <code class="literal">
        SHA()
       </code>
      </a>
      is
          synonymous with
      <a class="link" href="encryption-functions.html#function_sha1">
       <code class="literal">
        SHA1()
       </code>
      </a>
      .
     </p>
     <p>
      The return value is a string in the connection character set.
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa93101863"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token function">SHA1</span><span class="token punctuation">(</span><span class="token string">'abc'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token string">'a9993e364706816aba3e25717850c26c9cd0d89d'</span></code></pre>
     </div>
     <p>
      <a class="link" href="encryption-functions.html#function_sha1">
       <code class="literal">
        SHA1()
       </code>
      </a>
      can be considered a
          cryptographically more secure equivalent of
      <a class="link" href="encryption-functions.html#function_md5">
       <code class="literal">
        MD5()
       </code>
      </a>
      . However, see the note
          regarding the MD5 and SHA-1 algorithms at the beginning this
          section.
     </p>
    </li>
    <li class="listitem">
     <a name="function_sha2">
     </a>
     <p>
      <a class="link" href="encryption-functions.html#function_sha2">
       <code class="literal">
        SHA2(
        <em class="replaceable">
         <code>
          str
         </code>
        </em>
        ,
        <em class="replaceable">
         <code>
          hash_length
         </code>
        </em>
        )
       </code>
      </a>
     </p>
     <a class="indexterm" name="idm46045199282464">
     </a>
     <p>
      Calculates the SHA-2 family of hash functions (SHA-224,
          SHA-256, SHA-384, and SHA-512). The first argument is the
          plaintext string to be hashed. The second argument indicates
          the desired bit length of the result, which must have a value
          of 224, 256, 384, 512, or 0 (which is equivalent to 256). If
          either argument is
      <code class="literal">
       NULL
      </code>
      or the hash length
          is not one of the permitted values, the return value is
      <code class="literal">
       NULL
      </code>
      . Otherwise, the function result is a
          hash value containing the desired number of bits. See the
          notes at the beginning of this section about storing hash
          values efficiently.
     </p>
     <p>
      The return value is a string in the connection character set.
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa15256203"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token function">SHA2</span><span class="token punctuation">(</span><span class="token string">'abc'</span><span class="token punctuation">,</span> <span class="token number">224</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token string">'23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7'</span></code></pre>
     </div>
     <p>
      This function works only if MySQL has been configured with SSL
          support. See
      <a class="xref" href="encrypted-connections.html" title="8.3 Using Encrypted Connections">
       Section 8.3, “Using Encrypted Connections”
      </a>
      .
     </p>
     <p>
      <a class="link" href="encryption-functions.html#function_sha2">
       <code class="literal">
        SHA2()
       </code>
      </a>
      can be considered
          cryptographically more secure than
      <a class="link" href="encryption-functions.html#function_md5">
       <code class="literal">
        MD5()
       </code>
      </a>
      or
      <a class="link" href="encryption-functions.html#function_sha1">
       <code class="literal">
        SHA1()
       </code>
      </a>
      .
     </p>
    </li>
    <li class="listitem">
     <a name="function_statement-digest">
     </a>
     <p>
      <a class="link" href="encryption-functions.html#function_statement-digest">
       <code class="literal">
        STATEMENT_DIGEST(
        <em class="replaceable">
         <code>
          statement
         </code>
        </em>
        )
       </code>
      </a>
     </p>
     <a class="indexterm" name="idm46045199264224">
     </a>
     <p>
      Given an SQL statement as a string, returns the statement
          digest hash value as a string in the connection character set,
          or
      <code class="literal">
       NULL
      </code>
      if the argument is
      <code class="literal">
       NULL
      </code>
      . The related
      <a class="link" href="encryption-functions.html#function_statement-digest-text">
       <code class="literal">
        STATEMENT_DIGEST_TEXT()
       </code>
      </a>
      function returns the normalized statement digest. For
          information about statement digesting, see
      <a class="xref" href="performance-schema-statement-digests.html" title="29.10 Performance Schema Statement Digests and Sampling">
       Section 29.10, “Performance Schema Statement Digests and Sampling”
      </a>
      .
     </p>
     <p>
      Both functions use the MySQL parser to parse the statement. If
          parsing fails, an error occurs. The error message includes the
          parse error only if the statement is provided as a literal
          string.
     </p>
     <p>
      The
      <a class="link" href="server-system-variables.html#sysvar_max_digest_length">
       <code class="literal">
        max_digest_length
       </code>
      </a>
      system
          variable determines the maximum number of bytes available to
          these functions for computing normalized statement digests.
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa92891910"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token prompt">mysql&gt;</span> <span class="token keyword">SET</span> <span class="token variable">@stmt</span> <span class="token operator">=</span> <span class="token string">'SELECT * FROM mytable WHERE cola = 10 AND colb = 20'</span><span class="token punctuation">;</span>
<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token function">STATEMENT_DIGEST</span><span class="token punctuation">(</span><span class="token variable">@stmt</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> STATEMENT_DIGEST(@stmt)                                          <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> 3bb95eeade896657c4526e74ff2a2862039d0a0fe8a9e7155b5fe492cbd78387 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token function">STATEMENT_DIGEST_TEXT</span><span class="token punctuation">(</span><span class="token variable">@stmt</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> STATEMENT_DIGEST_TEXT(@stmt)                             <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> SELECT * FROM `mytable` WHERE `cola` = ? AND `colb` = ?  <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span></code></pre>
     </div>
    </li>
    <li class="listitem">
     <a name="function_statement-digest-text">
     </a>
     <p>
      <a class="link" href="encryption-functions.html#function_statement-digest-text">
       <code class="literal">
        STATEMENT_DIGEST_TEXT(
        <em class="replaceable">
         <code>
          statement
         </code>
        </em>
        )
       </code>
      </a>
     </p>
     <a class="indexterm" name="idm46045199245984">
     </a>
     <p>
      Given an SQL statement as a string, returns the normalized
          statement digest as a string in the connection character set,
          or
      <code class="literal">
       NULL
      </code>
      if the argument is
      <code class="literal">
       NULL
      </code>
      . For additional discussion and
          examples, see the description of the related
      <a class="link" href="encryption-functions.html#function_statement-digest">
       <code class="literal">
        STATEMENT_DIGEST()
       </code>
      </a>
      function.
     </p>
    </li>
    <li class="listitem">
     <a name="function_uncompress">
     </a>
     <p>
      <a class="link" href="encryption-functions.html#function_uncompress">
       <code class="literal">
        UNCOMPRESS(
        <em class="replaceable">
         <code>
          string_to_uncompress
         </code>
        </em>
        )
       </code>
      </a>
     </p>
     <a class="indexterm" name="idm46045199236288">
     </a>
     <p>
      Uncompresses a string compressed by the
      <a class="link" href="encryption-functions.html#function_compress">
       <code class="literal">
        COMPRESS()
       </code>
      </a>
      function. If the
          argument is not a compressed value, the result is
      <code class="literal">
       NULL
      </code>
      ; if
      <em class="replaceable">
       <code>
        string_to_uncompress
       </code>
      </em>
      is
      <code class="literal">
       NULL
      </code>
      , the result is also
      <code class="literal">
       NULL
      </code>
      . This function requires MySQL to have
          been compiled with a compression library such as
      <code class="literal">
       zlib
      </code>
      . Otherwise, the return value is always
      <code class="literal">
       NULL
      </code>
      .
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa59641139"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token function">UNCOMPRESS</span><span class="token punctuation">(</span><span class="token function">COMPRESS</span><span class="token punctuation">(</span><span class="token string">'any string'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token string">'any string'</span>
<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token function">UNCOMPRESS</span><span class="token punctuation">(</span><span class="token string">'any string'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token boolean">NULL</span></code></pre>
     </div>
    </li>
    <li class="listitem">
     <a name="function_uncompressed-length">
     </a>
     <p>
      <a class="link" href="encryption-functions.html#function_uncompressed-length">
       <code class="literal">
        UNCOMPRESSED_LENGTH(
        <em class="replaceable">
         <code>
          compressed_string
         </code>
        </em>
        )
       </code>
      </a>
     </p>
     <a class="indexterm" name="idm46045199219760">
     </a>
     <p>
      Returns the length that the compressed string had before being
          compressed. Returns
      <code class="literal">
       NULL
      </code>
      if
      <em class="replaceable">
       <code>
        compressed_string
       </code>
      </em>
      is
      <code class="literal">
       NULL
      </code>
      .
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa34864714"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token function">UNCOMPRESSED_LENGTH</span><span class="token punctuation">(</span><span class="token function">COMPRESS</span><span class="token punctuation">(</span><span class="token function">REPEAT</span><span class="token punctuation">(</span><span class="token string">'a'</span><span class="token punctuation">,</span><span class="token number">30</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token number">30</span></code></pre>
     </div>
    </li>
    <li class="listitem">
     <a name="function_validate-password-strength">
     </a>
     <p>
      <a class="link" href="encryption-functions.html#function_validate-password-strength">
       <code class="literal">
        VALIDATE_PASSWORD_STRENGTH(
        <em class="replaceable">
         <code>
          str
         </code>
        </em>
        )
       </code>
      </a>
     </p>
     <a class="indexterm" name="idm46045199207680">
     </a>
     <p>
      Given an argument representing a plaintext password, this
          function returns an integer to indicate how strong the
          password is, or
      <code class="literal">
       NULL
      </code>
      if the argument is
      <code class="literal">
       NULL
      </code>
      . The return value ranges from 0 (weak)
          to 100 (strong).
     </p>
     <p>
      Password assessment by
      <a class="link" href="encryption-functions.html#function_validate-password-strength">
       <code class="literal">
        VALIDATE_PASSWORD_STRENGTH()
       </code>
      </a>
      is
          done by the
      <code class="literal">
       validate_password
      </code>
      component. If
          that component is not installed, the function always returns
          0. For information about installing
      <code class="literal">
       validate_password
      </code>
      , see
      <a class="xref" href="validate-password.html" title="8.4.3 The Password Validation Component">
       Section 8.4.3, “The Password Validation Component”
      </a>
      . To examine or configure
          the parameters that affect password testing, check or set the
          system variables implemented by
      <code class="literal">
       validate_password
      </code>
      . See
      <a class="xref" href="validate-password-options-variables.html" title="8.4.3.2 Password Validation Options and Variables">
       Section 8.4.3.2, “Password Validation Options and Variables”
      </a>
      .
     </p>
     <p>
      The password is subjected to increasingly strict tests and the
          return value reflects which tests were satisfied, as shown in
          the following table. In addition, if the
      <a class="link" href="validate-password-options-variables.html#sysvar_validate_password.check_user_name">
       <code class="literal">
        validate_password.check_user_name
       </code>
      </a>
      system variable is enabled and the password matches the user
          name,
      <a class="link" href="encryption-functions.html#function_validate-password-strength">
       <code class="literal">
        VALIDATE_PASSWORD_STRENGTH()
       </code>
      </a>
      returns 0 regardless of how other
      <code class="literal">
       validate_password
      </code>
      system variables are set.
     </p>
     <div class="informaltable">
      <table summary="Password tests of the VALIDATE_PASSWORD_STRENGTH() function and the values returned by each password test.">
       <colgroup>
        <col style="width: 60%"/>
        <col style="width: 20%"/>
       </colgroup>
       <thead>
        <tr>
         <th>
          Password Test
         </th>
         <th>
          Return Value
         </th>
        </tr>
       </thead>
       <tbody>
        <tr>
         <td>
          Length &lt; 4
         </td>
         <td>
          0
         </td>
        </tr>
        <tr>
         <td>
          Length ≥ 4 and &lt;
          <a class="link" href="validate-password-options-variables.html#sysvar_validate_password.length">
           <code class="literal">
            validate_password.length
           </code>
          </a>
         </td>
         <td>
          25
         </td>
        </tr>
        <tr>
         <td>
          Satisfies policy 1 (
          <code class="literal">
           LOW
          </code>
          )
         </td>
         <td>
          50
         </td>
        </tr>
        <tr>
         <td>
          Satisfies policy 2 (
          <code class="literal">
           MEDIUM
          </code>
          )
         </td>
         <td>
          75
         </td>
        </tr>
        <tr>
         <td>
          Satisfies policy 3 (
          <code class="literal">
           STRONG
          </code>
          )
         </td>
         <td>
          100
         </td>
        </tr>
       </tbody>
      </table>
     </div>
    </li>
   </ul>
  </div>
 </div>
 <br/>
</div>
