GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2021-06-12 18:03:18

Juju
Member
Registered: 2015-10-01
Posts: 45

SHA1 <-> buffer

For GMS2.3.2

Expand/// Writes a SHA1 string to a buffer as individual bytes, from left to right in the SHA1 string
/// 
/// @return Nothing (undefined)
/// @param buffer      Buffer to write to
/// @param sha1String  SHA1 string to write

function buffer_write_sha1(_buffer, _sha1_string)
{
    var _i = 1;
    repeat(5)
    {
        //Did you know real() worked on hex?
        var _value = real("0x" + string_copy(_sha1_string, _i, 8));
        
        //Reverse bytes in the 32-bit word
        //We do this so that the bytes in the buffer correspond exactly to the bytes in the SHA1 string
        _value = (((_value & 0xFF) << 24) | ((_value & 0xFF00) << 8) | ((_value >> 8) & 0xFF00) | (_value >> 24));
        
        buffer_write(_buffer, buffer_u32, _value);
        
        _i += 8;
    }
}
Expand/// Reads a SHA1 hash from a buffer and converts it into a string
/// 
/// @return SHA1 string extracted from the buffer
/// @param buffer   Buffer to read from
/// @param [lower]  Optional. Whether to force the SHA1 string into lowercase for easier comparison with other SHA1 strings. Defaults to <true>

function buffer_read_sha1()
{
    var _buffer = argument[0];
    var _lower  = ((argument_count > 1) && (argument[1] != undefined))? argument[1] : true;
    
    var _sha1_string = "";
    
    repeat(5)
    {
        var _value = buffer_read(_buffer, buffer_u32);
        
        //Reverse bytes in the 32-bit word we pulled out
        _value = (((_value & 0xFF) << 24) | ((_value & 0xFF00) << 8) | ((_value >> 8) & 0xFF00) | (_value >> 24));
        
        //Checky hack with pointers to output a hex string
        _sha1_string += string(ptr(_value));
    }
    
    //If necessary, force lowercase abcdef
    if (_lower) _sha1_string = string_lower(_sha1_string);
    
    return _sha1_string;
}

Last edited by Juju (2021-06-12 18:03:35)

Offline

#2 2021-06-15 16:21:18

xot
Administrator
Registered: 2007-08-18
Posts: 1,238

Re: SHA1 <-> buffer

Thanks, Juju.

Whoa! Real works on hex now. When did that happen?

This:

Expandfunction buffer_read_sha1()
{
    var _buffer = argument[0];
    var _lower  = ((argument_count > 1) && (argument[1] != undefined))? argument[1] : true;

...can be this very soon:

Expandfunction buffer_read_sha1(_buffer, _lower=true)
{

Any thoughts about that? As soon as the current Beta moves to Stable I'm thinking this is way forward here. Unfortunately, this breaks with outdated Runtimes.


Abusing forum power since 1986.

Offline

Board footer

Powered by FluxBB