cryptohelpers.go (1192B)
1 // Copyright (c) 2017-2021 Ivan Jelincic <parazyd@dyne.org> 2 // 3 // This file is part of tordam 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 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 Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <https://www.gnu.org/licenses/>. 17 18 package tordam 19 20 import ( 21 "crypto/rand" 22 "encoding/base64" 23 "fmt" 24 ) 25 26 // RandomGarbage returns a base64 encoded string of n bytes of entropy. 27 func RandomGarbage(n int) (string, error) { 28 garbage := make([]byte, n) 29 read, err := rand.Read(garbage) 30 if err != nil { 31 return "", err 32 } 33 if n != read { 34 return "", fmt.Errorf("read %d, but requested %d bytes", read, n) 35 } 36 return base64.StdEncoding.EncodeToString(garbage), nil 37 }