C Generate Unique Key Based On String
C Generate Unique Key Based On String 7,9/10 4709 votes
Hi, Requirement: I need to create some unique key once in every 10 sec. My C code is in a loop, and after every 10 sec I have to generate some unique Unique Sequence Number Generator in C. I want to generate a unique ID for every instance of a given class. The ID is of type unsigned long When an instance is deleted, the ID is freed (can be reused) Now, I've come up with the following. Generate a unique numeric identifier for each instance of a class. Ask Question Asked 2 years, 7 months ago. /using-half-life-to-generate-keys.html. Vietcong 2 cd key generator. Making statements based on.
C Generate Unique Key Based On String Lights
Hi Barun Parichha,
This is not my field of expertise but I may be able to help?
I assume that you are looking for a 16-digit (not 16 byte) hex number in 4x2 byte decimated hex notation to denote a unique number? I also assume that this unique number MUST be unique from all other numbers generated? Something like a serial number generator, but in 4x2 hex decimated notation.
Your idea of using a random number generator, seeded by current time, although innovative, I don't believe will guarantee you a unique number, with respect to numbers previously generated.
Your second suggestion would be the method that I would consider, especially if you have the flexibility of 8 bytes (16 hex digits) to play with. Providing the unique number is only ever generated on a single system where you can guarantee that the clock will always be accurate in terms of ‘real world’ time, timezone and daylight savings etc, this should guarantee you a unique number. Before I go further, 2 explanations are required:
1. Single system: If you have multiple systems generating a number based purely on time, there is always the chance that both systems may generate the same number. If you are using multiple systems, you will need to consider this and perhaps incorporate a unique feature of each system into the number generation equation, such as MAC address etc.
2. Real world time: Should the system generating this unique number, based on time, not be accurate with its time keeping, there is the risk of producing duplicate numbers. Also be aware of daylight savings and its time transitions!
Assuming a single system with good clock keeping skills, no daylight saving, single timezone and delays of at least 10 seconds, to get a simple 4 bytes of 'unique' hex output, my approach might be:
#include <stdio.h>
#include <ctype.h>
#include <sys/time.h>
void stoupper (char *src) {
// Convert chars in string to upper case
while (*src != '0')
*(src++) = toupper (*src);
}
int main (int argc, char *argv[]) {
struct timeval tv;
struct timezone tz;
char seq[6];
union {
unsigned int t;
unsigned char ct[4];
} u;
gettimeofday (&tv,&tz);
u.t=(unsigned int)tv.tv_sec;
// Number of seconds since the Epoch – 1 Jan 1970 00:00:00
// Write time as 2x4 byte hex values
// Include byte-reversal for Intel architecture
sprintf (seq,'%02x%02x.%02x%02x',(unsigned char)u.ct[3],(unsigned char)u.ct[2],(unsigned char)u.ct[1],(unsigned char)u.ct[0]);
// Convert the hex values to upper case
stoupper (seq);
printf ('8bit hex time = (%s)n',seq);
return (0);
}
You still have 4 bytes to use if you wish to incorporate multiple systems, time issues etc.
I hope that helps,
BitBuster.
This is not my field of expertise but I may be able to help?
I assume that you are looking for a 16-digit (not 16 byte) hex number in 4x2 byte decimated hex notation to denote a unique number? I also assume that this unique number MUST be unique from all other numbers generated? Something like a serial number generator, but in 4x2 hex decimated notation.
Your idea of using a random number generator, seeded by current time, although innovative, I don't believe will guarantee you a unique number, with respect to numbers previously generated.
Your second suggestion would be the method that I would consider, especially if you have the flexibility of 8 bytes (16 hex digits) to play with. Providing the unique number is only ever generated on a single system where you can guarantee that the clock will always be accurate in terms of ‘real world’ time, timezone and daylight savings etc, this should guarantee you a unique number. Before I go further, 2 explanations are required:
1. Single system: If you have multiple systems generating a number based purely on time, there is always the chance that both systems may generate the same number. If you are using multiple systems, you will need to consider this and perhaps incorporate a unique feature of each system into the number generation equation, such as MAC address etc.
2. Real world time: Should the system generating this unique number, based on time, not be accurate with its time keeping, there is the risk of producing duplicate numbers. Also be aware of daylight savings and its time transitions!
Assuming a single system with good clock keeping skills, no daylight saving, single timezone and delays of at least 10 seconds, to get a simple 4 bytes of 'unique' hex output, my approach might be:
#include <stdio.h>
#include <ctype.h>
#include <sys/time.h>
void stoupper (char *src) {
// Convert chars in string to upper case
while (*src != '0')
*(src++) = toupper (*src);
}
int main (int argc, char *argv[]) {
struct timeval tv;
struct timezone tz;
char seq[6];
union {
unsigned int t;
unsigned char ct[4];
} u;
gettimeofday (&tv,&tz);
u.t=(unsigned int)tv.tv_sec;
// Number of seconds since the Epoch – 1 Jan 1970 00:00:00
// Write time as 2x4 byte hex values
// Include byte-reversal for Intel architecture
sprintf (seq,'%02x%02x.%02x%02x',(unsigned char)u.ct[3],(unsigned char)u.ct[2],(unsigned char)u.ct[1],(unsigned char)u.ct[0]);
// Convert the hex values to upper case
stoupper (seq);
printf ('8bit hex time = (%s)n',seq);
return (0);
}
You still have 4 bytes to use if you wish to incorporate multiple systems, time issues etc.
I hope that helps,
BitBuster.