From nobody Sun Feb 8 17:36:48 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=1620404707; cv=none; d=zohomail.com; s=zohoarc; b=oALFSkd+xxOZh1IybBsN2//pEkTHt3d/y+yywTc5oju6AlR/jk93bDygTQF+hFIry4b4XYLjpahbSN7Rfk0CyN9o16dyOExNEg+lB162U3Zjvla0RuxmQ952ULlj4IGjy3aLq92iPA0tDDWIaDVhsKavtFFyWsfFyjW2Z5f10yY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1620404707; 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=T44h6BNLDj0JEcXThlepK/5qkFRYLxSDHG2ewmNGK4X/2nKhkd3s1hh27Tu0Let4Z+1wmETCamazpBA6Xkya/1kIDvWOEF3oswJRN6AMglc0tcNpJtCsHH3DRJvUgJRn0dmBWZSV/scSBPfnotRuarO2ORAVQHaKCjhQs3KExgk= 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 1620404707983564.7453335688272; Fri, 7 May 2021 09:25:07 -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-27-pxdLifTYOxmtqYXNJL1a7g-1; Fri, 07 May 2021 12:25:04 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id AC80E1008060; Fri, 7 May 2021 16:24:57 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 93CD060D01; Fri, 7 May 2021 16:24:56 +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 513D35534E; Fri, 7 May 2021 16:24:56 +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 147GOt8o003338 for ; Fri, 7 May 2021 12:24:55 -0400 Received: by smtp.corp.redhat.com (Postfix) id 087B0189A5; Fri, 7 May 2021 16:24:55 +0000 (UTC) Received: from foo.redhat.com (ovpn-114-155.ams2.redhat.com [10.36.114.155]) by smtp.corp.redhat.com (Postfix) with ESMTP id ED01E60636; Fri, 7 May 2021 16:24:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1620404706; 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=BkSSsGeMXIFO4Jh9ZlJiKpKxOH5p2hBe3GcrS1HEie3e28XQDci4o9E/NKAyiEorKK9e5G Pq7jt0VV2rsJ9WaxEdSDQRMaZm4fomjoKWGIAguMhvIxqN4kknUl/2sy/83zogLyDcnK7z s77/jUO761nolMFYPiaZ6pxSar/uXnU= X-MC-Unique: pxdLifTYOxmtqYXNJL1a7g-1 From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Subject: [libvirt PATCH v2 01/10] util: add virRandomToken API Date: Fri, 7 May 2021 17:24:39 +0100 Message-Id: <20210507162448.660074-2-berrange@redhat.com> In-Reply-To: <20210507162448.660074-1-berrange@redhat.com> References: <20210507162448.660074-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.13 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