Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1 | /* |
Joey Armstrong | 9cdee9f | 2024-01-03 04:56:14 -0500 | [diff] [blame] | 2 | * Copyright 2018-2024 Open Networking Foundation (ONF) and the ONF Contributors |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 3 | |
Joey Armstrong | 7f8436c | 2023-07-09 20:23:27 -0400 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 7 | |
Joey Armstrong | 7f8436c | 2023-07-09 20:23:27 -0400 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 9 | |
Joey Armstrong | 7f8436c | 2023-07-09 20:23:27 -0400 | [diff] [blame] | 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 15 | */ |
| 16 | package common |
| 17 | |
| 18 | import ( |
| 19 | "fmt" |
| 20 | "math/rand" |
| 21 | "time" |
| 22 | ) |
| 23 | |
Joey Armstrong | 7f8436c | 2023-07-09 20:23:27 -0400 | [diff] [blame] | 24 | // GetRandomSerialNumber returns a serial number formatted as "HOST:PORT" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 25 | func GetRandomSerialNumber() string { |
| 26 | rand.Seed(time.Now().UnixNano()) |
| 27 | return fmt.Sprintf("%d.%d.%d.%d:%d", |
| 28 | rand.Intn(255), |
| 29 | rand.Intn(255), |
| 30 | rand.Intn(255), |
| 31 | rand.Intn(255), |
| 32 | rand.Intn(9000)+1000, |
| 33 | ) |
| 34 | } |
| 35 | |
Joey Armstrong | 7f8436c | 2023-07-09 20:23:27 -0400 | [diff] [blame] | 36 | // GetRandomMacAddress returns a random mac address |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 37 | func GetRandomMacAddress() string { |
| 38 | rand.Seed(time.Now().UnixNano()) |
| 39 | return fmt.Sprintf("%02x:%02x:%02x:%02x:%02x:%02x", |
| 40 | rand.Intn(128), |
| 41 | rand.Intn(128), |
| 42 | rand.Intn(128), |
| 43 | rand.Intn(128), |
| 44 | rand.Intn(128), |
| 45 | rand.Intn(128), |
| 46 | ) |
| 47 | } |
| 48 | |
| 49 | const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 50 | const ( |
| 51 | letterIdxBits = 6 // 6 bits to represent a letter index |
| 52 | letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits |
| 53 | letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits |
| 54 | ) |
| 55 | |
| 56 | var src = rand.NewSource(time.Now().UnixNano()) |
| 57 | |
| 58 | func GetRandomString(n int) string { |
| 59 | b := make([]byte, n) |
| 60 | // A src.Int63() generates 63 random bits, enough for letterIdxMax characters! |
| 61 | for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; { |
| 62 | if remain == 0 { |
| 63 | cache, remain = src.Int63(), letterIdxMax |
| 64 | } |
| 65 | if idx := int(cache & letterIdxMask); idx < len(letterBytes) { |
| 66 | b[i] = letterBytes[idx] |
| 67 | i-- |
| 68 | } |
| 69 | cache >>= letterIdxBits |
| 70 | remain-- |
| 71 | } |
| 72 | return string(b) |
| 73 | } |