github.com/meulengracht/snapd@v0.0.0-20210719210640-8bde69bcc84e/randutil/crypto.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2020 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package randutil
    21  
    22  import (
    23  	cryptorand "crypto/rand"
    24  	"encoding/base64"
    25  	"fmt"
    26  	"io/ioutil"
    27  	"strings"
    28  )
    29  
    30  // CryptoTokenBytes returns a crypthographically secure token of
    31  // nbytes random bytes.
    32  func CryptoTokenBytes(nbytes int) ([]byte, error) {
    33  	b := make([]byte, nbytes)
    34  	_, err := cryptorand.Read(b)
    35  	if err != nil {
    36  		return nil, fmt.Errorf("cannot obtain %d crypto random bytes: %v", nbytes, err)
    37  	}
    38  	return b, nil
    39  }
    40  
    41  // CryptoToken returns a crypthographically secure token string
    42  // encoding nbytes random bytes.
    43  // The result is URL-safe.
    44  func CryptoToken(nbytes int) (string, error) {
    45  	b, err := CryptoTokenBytes(nbytes)
    46  	if err != nil {
    47  		return "", err
    48  	}
    49  	return base64.RawURLEncoding.EncodeToString(b), nil
    50  }
    51  
    52  // RandomKernelUUID will return a UUID from the kernel's procfs API at
    53  // /proc/sys/kernel/random/uuid. Only to be used in very specific uses, most
    54  // random code should use CryptoToken(Bytes) instead.
    55  func RandomKernelUUID() string {
    56  	b, err := ioutil.ReadFile("/proc/sys/kernel/random/uuid")
    57  	if err != nil {
    58  		panic("cannot read kernel generated uuid")
    59  	}
    60  	return strings.TrimSpace(string(b))
    61  }