feat: added secure random URL option
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
parent
b66c0f0e00
commit
22a7b5b3fc
4 changed files with 101 additions and 52 deletions
|
|
@ -14,7 +14,8 @@ static const char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW
|
|||
static const size_t charset_len = sizeof(charset) - 1;
|
||||
|
||||
// Length of randomly generated keys
|
||||
#define RANDOM_KEY_LENGTH 4
|
||||
#define RANDOM_KEY_LENGTH_SHORT 4
|
||||
#define RANDOM_KEY_LENGTH_LONG 16
|
||||
|
||||
/**
|
||||
* Type definition for the struct representing the current Trie.
|
||||
|
|
@ -83,9 +84,10 @@ bool ternarytrie_add(TernaryTrie* trie, const char* key, Entry *entry);
|
|||
*
|
||||
* @param trie
|
||||
* @param entry entry to add
|
||||
* @param secure whether to generate a longer, more secure random key
|
||||
* @return the generated key
|
||||
*/
|
||||
char *ternarytrie_add_random(TernaryTrie *trie, Entry *entry);
|
||||
char *ternarytrie_add_random(TernaryTrie *trie, Entry *entry, bool secure);
|
||||
|
||||
/**
|
||||
* Remove an entry from this trie given its key.
|
||||
|
|
|
|||
|
|
@ -333,19 +333,20 @@ bool ternarytrie_add(TernaryTrie *trie, const char *key, Entry *entry) {
|
|||
return return_value;
|
||||
}
|
||||
|
||||
char* ternarytrie_add_random(TernaryTrie *trie, Entry *entry) {
|
||||
char* ternarytrie_add_random(TernaryTrie *trie, Entry *entry, bool secure) {
|
||||
pthread_rwlock_wrlock(&trie->lock);
|
||||
|
||||
// Generate random key
|
||||
bool ok = false;
|
||||
char *key = malloc(RANDOM_KEY_LENGTH + 1);
|
||||
key[RANDOM_KEY_LENGTH] = '\0';
|
||||
int key_length = secure ? RANDOM_KEY_LENGTH_LONG : RANDOM_KEY_LENGTH_SHORT;
|
||||
char *key = malloc(key_length + 1);
|
||||
key[key_length] = '\0';
|
||||
|
||||
// We naively generate new keys until we find a key that isn't in the trie
|
||||
// yet. With charset_len ** RANDOM_KEY_LENGTH sufficiently large, this isn't a
|
||||
// problem, because the chances of collisions are extremely small.
|
||||
while (!ok) {
|
||||
for (int i = 0; i < RANDOM_KEY_LENGTH; i++) {
|
||||
for (int i = 0; i < key_length; i++) {
|
||||
key[i] = charset[rand() % charset_len];
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue