Randomally generated code lazarus / 21-General ---------------------------------------------------------------------- BSaidus · 2025-04-28 09:57 Hello. I wonder if there such function exists. The function must generate code based on some what (date/datetime/time) the code generated change every call to this function BUT, when the code exchanged to other machine (via TCP/IP) then seconde machine must generate code using the same function and do comparison.   [machine1] => code1 ---->(tcp/ip)----->[machine2] => code2                       ==> (code1 = code2) ??                       I thought using datetime (the code change every 1 minute) to allow seconde machine to calculate the same value. If someone have an idea, I'll be happy thank you. ---------------------------------------------------------------------- Fibonacci · 2025-04-28 10:09 OTP? https://github.com/fibodevy/fpc-otp ---------------------------------------------------------------------- BSaidus · 2025-04-28 11:51 Thanks, but the FPC 3.2.2 and 3.2.3 do not support Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i reference to function ---------------------------------------------------------------------- Fibonacci · 2025-04-28 11:57 Well, you can use the code at the commit before I removed the "Fundamentals" units for SHA1/SHA256: https://github.com/fibodevy/fpc- otp/tree/fd38f66799d0ce2c1782eba73719242b721d2c49 ---------------------------------------------------------------------- rvk · 2025-04-28 12:07 If it's randomly generated on one machine... you should never be able to generate this on the second (otherwise it's not random). What do you want to use this for? Sending 202504281406 is also a code... with date and time and which can be re-generated on the other machine. So you need to make clear what you want and where you want to use it for (or against). ---------------------------------------------------------------------- Thaddy · 2025-04-28 12:20 You can use my UUIDv7 code for that. Actually that is what is is for, It generates a UUID containing a timestamp in UTC, so that independent of location or time zone applications can exchange data, as long as the other side receives the same UUIDv7. The UUIDv7 has 74 bit random entropy apart from the timestamp and versioning, the random part can be used as a key and the time part for time based sorting. ---------------------------------------------------------------------- rvk · 2025-04-28 12:24 Quote from: Thaddy on April 28, 2025, 02:20:16 pmThe UUIDv has 74 bit random entropy apart from the timestamp and the random part can be used as a key and the time part for time based sorting. Good luck re-generating that exact UUIDv with random part on another machine "EXACTLY". That's what OP is asking... to re-generate code2 on a second machine where code1 = code2. I don't see the point in doing this. ---------------------------------------------------------------------- Thaddy · 2025-04-28 12:28 If the UUIDv7 is send to the other side it can get back the timestamp received, the exact information from, say, a database, the correct record. That's what this particular UUID is for. It is always incremental. ---------------------------------------------------------------------- rvk · 2025-04-28 12:30 Quote from: Thaddy on April 28, 2025, 02:28:40 pmIf the UUIDv7 is send to the other side it can get back the timestamp received, the exact information from, say, a database, the correct record. That's what this particular UUID is for. It is always incremental. So it's not "random" like OP asked. (see my point... there is no point in the question...) ---------------------------------------------------------------------- Fibonacci · 2025-04-28 12:40 If the OP wants to secure the communication against replay attacks, just add a timestamp (or a counter) to the packet, and then add salted hash of the packet. But what he asked for sounded like OTP is the answer. ---------------------------------------------------------------------- BSaidus · 2025-04-28 12:50 Quote from: Fibonacci on April 28, 2025, 02:40:41 pmIf the OP wants to secure the communication against replay attacks, just add a timestamp (or a counter) to the packet, and then add salted hash of the packet. But what he asked for sounded like OTP is the answer. Thanks for this code, it works well. Indeed, I want to secure communication with this code so the receiver can decode it (but It must change on every time I send data ) the code sent must change say every 10s MAX ( since the application is in local area). @Thaddy I'll try to play with it & see, thanks. Anyway, the function I want, must calculate code in client side, then send data with this code, when the server application intercept, It do the same calculos with the same function and compare it with the code sent from the client. If it equals then OK. Any other suggestion is welcom. Thanks ---------------------------------------------------------------------- Thaddy · 2025-04-28 12:55 Quote from: rvk on April 28, 2025, 02:30:24 pmQuote from: Thaddy on April 28, 2025, 02:28:40 pmIf the UUIDv7 is send to the other side it can get back the timestamp received, the exact information from, say, a database, the correct record. That's what this particular UUID is for. It is always incremental. So it's not "random" like OP asked. (see my point... there is no point in the question...) No I don't see the point: it is 74 bit secure random and changes per ms. ---------------------------------------------------------------------- rvk · 2025-04-28 14:04 Quote from: BSaidus on April 28, 2025, 02:50:18 pmAnyway, the function I want, must calculate code in client side, then send data with this code, when the server application intercept, It do the same calculos with the same function and compare it with the code sent from the client. If it equals then OK. Still not sure how the client, which receives the code, would check if the code is valid, and not from any other computer. What is the function? So it's no problem if ANY computer can send such code, as long as it's calculated the same way?? (then you can just send 250428160410{+random number}, rounded at 10 seconds. anyone can do that and you can check that 'code') Or are you trying to create some security? So only the client with some sort of key sends this data? Because then you would also need to build in some public/private key part (or alike). So the question is... is this for security... or just for sending a timestamp (within 10 seconds frame)? ---------------------------------------------------------------------- Thaddy · 2025-04-28 15:12 Actually, you do not get it: He needs predicatble timing together with strong random. ---------------------------------------------------------------------- rvk · 2025-04-28 15:16 QuoteBUT, when the code exchanged to other machine (via TCP/IP) then seconde machine must generate code using the same function and do comparison.   [machine1] => code1 ---->(tcp/ip)----->[machine2] => code2                       ==> (code1 = code2) ??How can you ever do that if code1 is random? Random implies random, so you can't regenerate it. So you can never regenerate code2 to be the same as code1. UNLESS code1 isn't really random. ---------------------------------------------------------------------- BSaidus · 2025-04-28 17:57 Quote from: rvk on April 28, 2025, 05:16:26 pmQuoteBUT, when the code exchanged to other machine (via TCP/IP) then seconde machine must generate code using the same function and do comparison.   [machine1] => code1 ---->(tcp/ip)----->[machine2] => code2                       ==> (code1 = code2) ??How can you ever do that if code1 is random? Random implies random, so you can't regenerate it. So you can never regenerate code2 to be the same as code1. UNLESS code1 isn't really random. I think there is misunderstanding ( I did not express the needs exactly ) What I mean by random is: writing a function that generate a number, string or code that can be send using TCP/IP protocol then when the server app catch data, It uses the same function to calculate (generate) a number, string or code, then compared to one sent by client side.    I want just control that the communication come from my own client. PS: this function must generate different codes every time I call it (I think the best way is to be based on datetime). That's all. Hope @rvk understand ---------------------------------------------------------------------- rvk · 2025-04-28 18:07 Quote from: BSaidus on April 28, 2025, 07:57:26 pm   I want just control that the communication come from my own client. PS: this function must generate different codes every time I call it. Yes, but the question is how secure does it need to be. Like I said before... You can just use a datetime string (rounded to 10 seconds) with something (for example 'aa' prefixed). So aa20250428200210.123 and your client knows this comes from a client. Is that enough? Or do you have more requirements. You can also just use the Unix timestamp (rounded to 10 seconds). It's very easy to 'hack'. As I, as a rogue computer, can just forge such messages. If that's not a problem... Just use any format of datetime you like. (Int64 with unixtime or just a ASCII string with mentioned time code). Knowing if a certain message comes from your own program is just a matter of creating your own format/protocol. If it starts with abcd{etc},datetimecode,your message it is a valid message. You see, you can just create your own 'protocol' to recognize your own messages. (The same as you look in a zip file it always has PK\x03\x04 etc magic number in front of it.) ---------------------------------------------------------------------- BSaidus · 2025-04-28 20:55 Quote from: rvk on April 28, 2025, 08:07:59 pmQuote from: BSaidus on April 28, 2025, 07:57:26 pm   I want just control that the communication come from my own client. PS: this function must generate different codes every time I call it. Yes, but the question is how secure does it need to be. Like I said before... You can just use a datetime string (rounded to 10 seconds) with something (for example 'aa' prefixed). So aa20250428200210.123 and your client knows this comes from a client. Is that enough? Or do you have more requirements. You can also just use the Unix timestamp (rounded to 10 seconds). It's very easy to 'hack'. As I, as a rogue computer, can just forge such messages. If that's not a problem... Just use any format of datetime you like. (Int64 with unixtime or just a ASCII string with mentioned time code). Knowing if a certain message comes from your own program is just a matter of creating your own format/protocol. If it starts with abcd{etc},datetimecode,your message it is a valid message. You see, you can just create your own 'protocol' to recognize your own messages. (The same as you look in a zip file it always has PK\x03\x04 etc magic number in front of it.) All what you sayed is good, but, if one intercept the message data and try to resend some data formated as previous intercepted message, the code I want is one shot (generated using datetime) and transformed (hashed, crypted) with some mecanism to be unreadable by the interceptor ---------------------------------------------------------------------- rvk · 2025-04-28 21:06 That's why I was asking about security. So you want encryption. And don't leak the encryption method (and key) so others can use it to fake your messages. BTW. The timestamp is just one part of it. But the encryption is more important because a timestamp in an unencrypted message can always be faked. So you can just encrypt the message with a key (with your preferred encryption) and you need to make sure the key is only available for the client. ---------------------------------------------------------------------- Thaddy · 2025-04-29 11:48 Maybe this will help. The key is a number, any number that fits in 32 bits. Cycles is the number of cycles to repeat the encryption. You can send the key and optionally the number of cycles per sms. The other side needs a small executable that does the decryption or you can build it in the application: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   function Crypt(const Data:TBytes;Key:Cardinal;Cycles:Cardinal=1):TBytes;inline;  var    i,j:integer;   begin    Result := [];    Setlength(Result, Length(Data));    For i:= 0 to Pred(Cycles) do      for j:= 0 to high(Data) do      begin        { this is an lcg type prng, actually Delphi's }        Key := Key * 134775813  + 1;        Result[j] := Data[j] xor (Key * 256 shr 32);      end;  end;  // more secure  function CryptXos(const Data:TBytes;Key:Cardinal;Cycles:Cardinal=1):TBytes;inline;  var    i,j:integer;   begin    Result := [];    Setlength(Result, Length(Data));    For i:= 0 to Pred(Cycles) do      for j:= 0 to high(Data) do      begin        key := key xor(key shl 13);        key := key shr 17;        key := key xor (key shl 5);        result[j] := key;      end;  end; Just put your code in a TBytes. Then send the key and optionally the number of cycles per sms and the encrypted data over internet. The other side will be able to decrypt by calling the same function with the key and optionally the cycles from the sms. Code: [Select]trunk FPC 3.3.1-17789-g5952c5452b [2025/04/13] for x86_64 - Win64 tkUString Enter your pincode: 12345 Encrypt: This is thè téxt to êncrypt/decrypt Ä. Equivalent in bytes:    54  124  203    3   40  144   78  162  163  174  216  148   90  136    0  183  251  243  162  137  182  240  141   68  210  224   25  153  247  166  119  222  221  127  169  180  109  253 Decrypt: 䨶赼埋ᐃ劐쥎涢첣ෘ澔띚䢈က₷䟻훳ಢ쮉ຶ㗰튍迒쓠ꮙ૷䆦ꭷߞዝ챿ꖩ▴ㇽ Result:  This is thè téxt to êncrypt/decrypt Ä.If required I can easily adapt it to use an even more advanced (RSA) encryption but the XOS version is already very secure. In effect the key is the random seed. I also have a stream version. This encryption is symmetric, so the same function encrypts and decrypts. For normal people this is impossible to decrypt if you don't know the key and the cycles, it is way more advanced than simple XOR. Let me know if this fits your requirements. ---------------------------------------------------------------------- Thaddy · 2025-04-29 16:41 I have noticed a small bug in CryptXos regarding the string types, but the TBytes encrypt/Decrypt OK and is the best way to use anyway, I added the string types just for display purpose. Use the TBytes version directly.