HMAC SQL Server для функции MySQL - PullRequest
0 голосов
/ 22 января 2019

Я пытался подписать полезную нагрузку 4 подписи, и по разным причинам я не могу использовать один из AWS SDK.Я делал это раньше, вызывая приведенную ниже функцию SQL Server, но на этот раз я должен сделать это в MySQL.

Как я могу преобразовать эту функцию SQL Server в MySQL?Я попробовал себя, и я попробовал несколько онлайн-конвертеров.К сожалению, у меня не хватило времени, чтобы выучить MySQL достаточно хорошо, чтобы понять это.

DELIMITER //

CREATE FUNCTION HMAC (
    p_algo VARCHAR(20)
    ,p_key LONGBLOB
    ,p_data LONGBLOB
    )
    /* This function only takes VARBINARY parameters instead of VARCHAR
to prevent problems with implicit conversion from NVARCHAR to VARCHAR 
which result in incorrect hashes for inputs including non-ASCII characters. 
Always cast @key and @data parameters to VARBINARY when using this function. 
Tested against HMAC vectors for MD5 and SHA1 from RFC 2202 */

/*
List of secure hash algorithms (parameter @algo) supported by MSSQL 
version. This is what is passed to the HASHBYTES system function.
Omit insecure hash algorithms such as MD2 through MD5
2005-2008R2: SHA1 
2012-2016: SHA1 | SHA2_256 | SHA2_512 
*/
RETURNS VARBINARY(64)
BEGIN
    DECLARE v_ipad BIGINT;
    DECLARE v_opad BIGINT;
    DECLARE v_i VARBINARY(64);
    DECLARE v_o VARBINARY(64);
    DECLARE v_pos INTEGER;

    -- SQL 2005 only allows XOR operations on integer types, so use bigint and interate 8 times
    SET v_ipad = cast(0x3636363636363636 AS BIGINT); -- constants from HMAC definition
    SET v_opad = cast(0x5C5C5C5C5C5C5C5C AS BIGINT);

    IF char_length(rtrim(p_key)) > 64 THEN -- if the key is grater than 512 bits we hash it first per HMAC definition
        SET p_key = cast(hashbytes(p_algo, p_key) AS BINARY (64));
    ELSE
        SET p_key = cast(p_key AS BINARY (64));
    END IF; -- otherwise pad it out to 512 bits with zeros

    SET v_pos = 1;
    SET v_i = cast('' AS VARBINARY(64)); -- initialize as empty binary value

    WHILE v_pos <= 57
    DO
        SET v_i = v_i + cast((substring(p_key, v_pos, 8) ^; v_ipad) AS VARBINARY(64))
        SET v_pos = v_pos + 8;
    END WHILE;

    SET v_pos = 1;
    SET v_o = cast('' AS VARBINARY(64)); -- initialize as empty binary value

    WHILE v_pos <= 57
    DO
        SET v_o = v_o + cast((substring(p_key, v_pos, 8) ^; v_opad) AS VARBINARY(64))
        SET v_pos = v_pos + 8;
    END WHILE;

    RETURN hashbytes(p_algo, v_o + hashbytes(p_algo, v_i + p_data));
END;
//

DELIMITER ;




GRANT EXECUTE
    ON HMAC
    TO PUBLIC;


/*
Copyright © 2012 Ryan Malayter. All Rights Reserved. 
Redistribution and use in source and binary forms, with or without 
modification, are permitted provided that the following conditions are 
met: 
1. Redistributions of source code must retain the above copyright 
notice, this list of conditions and the following disclaimer. 
2. Redistributions in binary form must reproduce the above copyright 
notice, this list of conditions and the following disclaimer in the 
documentation and/or other materials provided with the distribution. 
3. The name of the author may not be used to endorse or promote products 
derived from this software without specific prior written permission. 
THIS SOFTWARE IS PROVIDED BY Ryan Malayter "AS IS" AND ANY EXPRESS OR 
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE. 
*/
...