GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2016-08-07 07:28:58

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

HMAC-SHA1 checksums and ds_map saving/loading

An example on how to save/load protected maps. Uses a private key (built from character codes rather than using a plaintext string). This is not encryption!

Download here (11kb .gmz, tested in v1.4.1757)

Depends on hmac_sha1()

///ds_map_hmac_sha1_save( id, filename, key )

var _map = argument0;
var _filename = argument1;
var _key = argument2;

var _str = ds_map_write( _map );
var _checksum = hmac_sha1( _key, _str );
var _buffer = buffer_create( string_length( _checksum ) + 1 + string_length( _str ) + 1, buffer_fixed, 1 );
buffer_write( _buffer, buffer_string, _checksum );
buffer_write( _buffer, buffer_string, _str );
buffer_save( _buffer, _filename );
buffer_delete( _buffer );

return true;
///ds_map_hmac_sha1_load( filename, key )

var _filename = argument0;
var _key = argument1;

if ( !file_exists( _filename ) ) {
    return undefined;
    show_debug_message( "ds_map_hmac_sha1_load: Error! File not found" );
}

var _buffer = buffer_load( _filename );

var _checksum = buffer_read( _buffer, buffer_string );
if ( buffer_tell( _buffer ) >= buffer_get_size( _buffer ) ) {
    show_debug_message( "ds_map_hmac_sha1_load: Error! Only one string found (this is usually when someone has editted the file in Notepad)" );
    return undefined;
}

var _str = buffer_read( _buffer, buffer_string );
if ( _checksum != hmac_sha1( _key, _str ) ) {
    show_debug_message( "ds_map_hmac_sha1_load: Error! Checksum does not match expected checksum" );
    return undefined;
} else {
    var _map = ds_map_create();
    ds_map_read( _map, _str );
    return _map;
}

Last edited by Juju (2016-08-07 07:47:14)

Offline

#2 2016-08-11 11:54:15

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

Re: HMAC-SHA1 checksums and ds_map saving/loading

Nice!


Abusing forum power since 1986.

Offline

Board footer

Powered by FluxBB