mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-08-28 11:31:16 +00:00
util: add virRandomToken API
A random token is simply a string of random bytes formatted in hexidecimal. Reviewed-by: Michal Privoznik <mprivozn@redhat.com> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
This commit is contained in:
parent
d6a1d1841a
commit
1ca3959712
@ -3081,6 +3081,7 @@ virRandomBits;
|
|||||||
virRandomBytes;
|
virRandomBytes;
|
||||||
virRandomGenerateWWN;
|
virRandomGenerateWWN;
|
||||||
virRandomInt;
|
virRandomInt;
|
||||||
|
virRandomToken;
|
||||||
|
|
||||||
|
|
||||||
# util/virresctrl.h
|
# util/virresctrl.h
|
||||||
|
@ -161,3 +161,21 @@ virRandomGenerateWWN(char **wwn,
|
|||||||
(unsigned long long)virRandomBits(36));
|
(unsigned long long)virRandomBits(36));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char *virRandomToken(size_t len)
|
||||||
|
{
|
||||||
|
g_autofree unsigned char *data = g_new0(unsigned char, len);
|
||||||
|
g_autofree char *token = g_new0(char, (len * 2) + 1);
|
||||||
|
static const char hex[] = "0123456789abcdef";
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
if (virRandomBytes(data, len) < 0)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
for (i = 0; i < len; i++) {
|
||||||
|
token[(i*2)] = hex[data[i] & 0xf];
|
||||||
|
token[(i*2)+1] = hex[(data[i] >> 4) & 0xf];
|
||||||
|
}
|
||||||
|
|
||||||
|
return g_steal_pointer(&token);
|
||||||
|
}
|
||||||
|
@ -26,3 +26,4 @@ uint32_t virRandomInt(uint32_t max);
|
|||||||
int virRandomBytes(unsigned char *buf, size_t buflen)
|
int virRandomBytes(unsigned char *buf, size_t buflen)
|
||||||
ATTRIBUTE_NONNULL(1) G_GNUC_WARN_UNUSED_RESULT G_GNUC_NO_INLINE;
|
ATTRIBUTE_NONNULL(1) G_GNUC_WARN_UNUSED_RESULT G_GNUC_NO_INLINE;
|
||||||
int virRandomGenerateWWN(char **wwn, const char *virt_type) G_GNUC_NO_INLINE;
|
int virRandomGenerateWWN(char **wwn, const char *virt_type) G_GNUC_NO_INLINE;
|
||||||
|
char *virRandomToken(size_t len);
|
||||||
|
Loading…
Reference in New Issue
Block a user