Raised This Month: $51 Target: $400
 12% 

String Encrypting Method


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
siriusmd99
Veteran Member
Join Date: Oct 2013
Location: Republic of Moldova
Old 08-19-2017 , 16:50   String Encrypting Method
Reply With Quote #1

Why would md5 hash string encryption be better and safer than my method?

Uniqueid = ( str[0] + str[1] + ... str[last char] ) * sqr(strlen(str))

Example :

str[ ] = "hello"

Uniqueid = (104 + 101 + 108 +108 + 111) * sqr(5) = 532 * 25 = 13300

Its simple and the strings are encrypted well. There cannot be when two strings have the same ID, because I do square operation to string length and the ID value grows geometrically.

So I wonder is there any leak in this method or something? I just made up this method and I don't want to complicate life with md5 or other stuff.

And also, I want to say that I don't want to make unique id for two equal strings but different timestamp. If I wanted that I could add get_systime() above the ID.
siriusmd99 is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 08-19-2017 , 17:03   Re: String Encrypting Method
Reply With Quote #2

That's not encrypting, that's hashing (as is MD5).

Quote:
There cannot be when two strings have the same ID, because I do square operation to string length and the ID value grows geometrically.
Subtract 1 from a character in the string, add 1 to another one (instead of 108 + 108 like in your example, have 107 + 109) and there - different strings, same ID.

Quote:
I don't want to complicate life with md5 or other stuff.
How is that complicating anything? Just call one function and get the hash of a given string. Wouldn't be much different than your method.

We wouldn't have a need for MD5 (and other much more advanced and better algorithms) if logic behind hashing was that basic.


Anyway, what are you actually trying to do? Why do you need this?

Last edited by klippy; 08-19-2017 at 17:05.
klippy is offline
siriusmd99
Veteran Member
Join Date: Oct 2013
Location: Republic of Moldova
Old 08-19-2017 , 17:45   Re: String Encrypting Method
Reply With Quote #3

Yes, I forgot about different characters and the same length. But I can sum squares of each character's code.
Anyway...
So I can use md5 hashing but I it stores has as string. What's the point then? I want to reduce memory use and store just an ID to SQL not a whole string. Md5 function in Amxx is copying hash result as string. I want integer instead.
siriusmd99 is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 08-19-2017 , 18:10   Re: String Encrypting Method
Reply With Quote #4

Quote:
Originally Posted by siriusmd99 View Post
Yes, I forgot about different characters and the same length. But I can sum squares of each character's code.
Anyway...
So I can use md5 hashing but I it stores has as string. What's the point then? I want to reduce memory use and store just an ID to SQL not a whole string. Md5 function in Amxx is copying hash result as string. I want integer instead.
str_to_num huh
__________________

Last edited by HamletEagle; 08-19-2017 at 18:10.
HamletEagle is offline
siriusmd99
Veteran Member
Join Date: Oct 2013
Location: Republic of Moldova
Old 08-19-2017 , 18:59   Re: String Encrypting Method
Reply With Quote #5

Quote:
Originally Posted by HamletEagle View Post
str_to_num huh
Nope. It has letters.

Something like a5485d82ae55dac
I can't use str to num here. Not all symbols are digits.

Last edited by siriusmd99; 08-19-2017 at 18:59.
siriusmd99 is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 08-19-2017 , 23:58   Re: String Encrypting Method
Reply With Quote #6

What are you actually trying to do with your string? As previously mentioned, a hash is not a method of encryption. You will never be able to get your original string from the hash.

Note that the string is actually a number represented as hexadecimal. So, it can be converted to a number. I don't think str_to_num() handles hex values.
__________________

Last edited by fysiks; 08-20-2017 at 00:02.
fysiks is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 08-20-2017 , 06:30   Re: String Encrypting Method
Reply With Quote #7

Quote:
Originally Posted by siriusmd99 View Post
Nope. It has letters.

Something like a5485d82ae55dac
I can't use str to num here. Not all symbols are digits.
You still can use str_to_num, but you won't be able to convert back from the number to the hash. If you want to get a number from the hash and then convert it back you could to a stupid trick like that:
PHP Code:
#include <amxmodx>
#include <fakemeta>

public plugin_init()
{
    new const 
String[] = "test"
    
new HashedString[34], OtherString[34]
    
md5(StringHashedString)
    
    new 
Offset engfunc(EngFunc_AllocStringHashedString//get the offset
    
    
global_get(glb_pStringBaseOffsetOtherStringcharsmax(OtherString))//convert back to a string
    
server_print("[%s] [%s] [%i] [%s]"StringHashedStringOffsetOtherString)
    

Output:
Code:
[test] [098f6bcd4621d373cade4e832627b4f6] [41524840] [098f6bcd4621d373cade4e832627b4f6]
Note that allocating a string is an expensive operation so depending on what you want to do it may not be a good idea. As fysiks said, you should explain what you are trying to do.
__________________

Last edited by HamletEagle; 08-20-2017 at 07:47.
HamletEagle is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 08-20-2017 , 07:27   Re: String Encrypting Method
Reply With Quote #8

MD5 outputs a 128-bit string, you can use a 4 element long array to represent it too.
klippy is offline
siriusmd99
Veteran Member
Join Date: Oct 2013
Location: Republic of Moldova
Old 08-20-2017 , 07:43   Re: String Encrypting Method
Reply With Quote #9

I don't need to convert it back. Just to get unique ID to save SQL.
Hash_column[0] = some hash
Hash_column[1] = some hash
And so on..
Then I read a text file and create an array to save hashes after I read string.
Like
fget string
Md5 hash
Str to num
And then save num to a global array
Hashes[i] = num

When player connects I get Hash_column from SQL and loop every ID to check if it's contained in Hashes[] global array.

So I just want str to num work without any engfunc and the generated integer id be unique so that there won't be different strings with same ID after using str to num. That's all.

Last edited by siriusmd99; 08-20-2017 at 07:51.
siriusmd99 is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 08-20-2017 , 08:01   Re: String Encrypting Method
Reply With Quote #10

Quote:
When player connects I get Hash_column from SQL and loop every ID to check if it's contained in Hashes[] global array.
It seems that you are looking for hash maps (Tries). Given that you are using SQL, something can probably done with SQL queries too.

This looks like an XY problem; better describe the problem itself, not what you think the solution to the problem is.

Last edited by klippy; 08-20-2017 at 08:03.
klippy is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 20:05.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode