From nobody Sun Feb 8 19:40:07 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) client-ip=216.205.24.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1620826456; cv=none; d=zohomail.com; s=zohoarc; b=YEBYhReauOkeOx0nGE00v+we50arvrVdf4r7T75rqM8YfDBiyHPJq4njSLG1Dj70N9BU+sNbIPL1hqiET7uQHcJDawtEKUSbtBHaTO7hQZXRtwWpE5/FPa5Jj0WafL3oej/7Stg+cfynYEynxpOE2vsFofuc976dYuxitBi8XyQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1620826456; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=YiKb+y371mNU6ngLCWNId09CEultisUOluzBj6IGhy0=; b=l7kYqSSxs9hbvIhRhGr4ueQ8c8MXonUKMBtf30lUeH1CuL+bxH7Pe9ksg9YFH6E2yapkKrLTekmC3grBNx9Of6C0ljy3cUYKJzC9YcV6muvSaVtXput92hX6pkW6wWt9hUlsvtYQHRlwG6w41lGWgCb9ZVNweJ7WzaWXZnNoRmw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by mx.zohomail.com with SMTPS id 162082645654583.41860273760881; Wed, 12 May 2021 06:34:16 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-467-oulDb90dOvqbRnZOarL3JA-1; Wed, 12 May 2021 09:34:12 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id D42D66D5CC; Wed, 12 May 2021 13:34:05 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B0F3B5D6AC; Wed, 12 May 2021 13:34:05 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 0BA2C1800BB0; Wed, 12 May 2021 13:34:05 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 14CDY3Md031293 for ; Wed, 12 May 2021 09:34:03 -0400 Received: by smtp.corp.redhat.com (Postfix) id 320E963BA7; Wed, 12 May 2021 13:34:03 +0000 (UTC) Received: from foo.redhat.com (ovpn-114-167.ams2.redhat.com [10.36.114.167]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2E0C260657; Wed, 12 May 2021 13:34:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1620826454; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=YiKb+y371mNU6ngLCWNId09CEultisUOluzBj6IGhy0=; b=YQxEhmcjJGfQpqeanXBZH9HBlcDk169Xpj9Mj8cV0dEfIOAAHUEbNf9PU8dMa6Qxm0VXyH 0+SCaiSFyQKClQl0NNNYLO7tQGxYOi+apAn8GIJgSvID1/jx18sswqJ/kKLKF51UXRCuob /NNF1fZW21bgGrj7AfuxnLAmvu8PEFA= X-MC-Unique: oulDb90dOvqbRnZOarL3JA-1 From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Subject: [libvirt PATCH v3 01/10] util: add virRandomToken API Date: Wed, 12 May 2021 14:33:47 +0100 Message-Id: <20210512133356.1162418-2-berrange@redhat.com> In-Reply-To: <20210512133356.1162418-1-berrange@redhat.com> References: <20210512133356.1162418-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com Cc: Michal Privoznik X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) A random token is simply a string of random bytes formatted in hexidecimal. Reviewed-by: Michal Privoznik Signed-off-by: Daniel P. Berrang=C3=A9 --- src/libvirt_private.syms | 1 + src/util/virrandom.c | 18 ++++++++++++++++++ src/util/virrandom.h | 1 + 3 files changed, 20 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 1b12c49018..23621fcfd0 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -3081,6 +3081,7 @@ virRandomBits; virRandomBytes; virRandomGenerateWWN; virRandomInt; +virRandomToken; =20 =20 # util/virresctrl.h diff --git a/src/util/virrandom.c b/src/util/virrandom.c index 3ae1297e6b..c3f3aa1fa6 100644 --- a/src/util/virrandom.c +++ b/src/util/virrandom.c @@ -161,3 +161,21 @@ virRandomGenerateWWN(char **wwn, (unsigned long long)virRandomBits(36)); return 0; } + +char *virRandomToken(size_t len) +{ + g_autofree unsigned char *data =3D g_new0(unsigned char, len); + g_autofree char *token =3D g_new0(char, (len * 2) + 1); + static const char hex[] =3D "0123456789abcdef"; + size_t i; + + if (virRandomBytes(data, len) < 0) + return NULL; + + for (i =3D 0; i < len; i++) { + token[(i*2)] =3D hex[data[i] & 0xf]; + token[(i*2)+1] =3D hex[(data[i] >> 4) & 0xf]; + } + + return g_steal_pointer(&token); +} diff --git a/src/util/virrandom.h b/src/util/virrandom.h index 297721f912..aac684ada9 100644 --- a/src/util/virrandom.h +++ b/src/util/virrandom.h @@ -26,3 +26,4 @@ uint32_t virRandomInt(uint32_t max); int virRandomBytes(unsigned char *buf, size_t buflen) ATTRIBUTE_NONNULL(1) G_GNUC_WARN_UNUSED_RESULT G_GNUC_NO_INLINE; int virRandomGenerateWWN(char **wwn, const char *virt_type) G_GNUC_NO_INLI= NE; +char *virRandomToken(size_t len); --=20 2.31.1