-
Notifications
You must be signed in to change notification settings - Fork 396
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1556 from openshift-cherrypick-robot/cherry-pick-…
…1548-to-release-0.6 [release-0.6] reconciler/apiexport: switch from rsa4096 to 256 bit cryptographic random string
- Loading branch information
Showing
8 changed files
with
61 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
Copyright 2022 The KCP Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package crypto | ||
|
||
import ( | ||
"encoding/base64" | ||
"math/rand" | ||
) | ||
|
||
// RandomBits returns a random byte slice with at least the requested bits of entropy. | ||
// Callers should avoid using a value less than 256 unless they have a very good reason. | ||
func RandomBits(bits int) []byte { | ||
size := bits / 8 | ||
if bits%8 != 0 { | ||
size++ | ||
} | ||
b := make([]byte, size) | ||
if _, err := rand.Read(b); err != nil { | ||
panic(err) // rand should never fail | ||
} | ||
return b | ||
} | ||
|
||
// RandomBitsString returns a random string with at least the requested bits of entropy. | ||
// It uses RawURLEncoding to ensure we do not get / characters or trailing ='s. | ||
func RandomBitsString(bits int) string { | ||
return base64.RawURLEncoding.EncodeToString(RandomBits(bits)) | ||
} | ||
|
||
// Random256BitsString is a convenience function for calling RandomBitsString(256). | ||
// Callers that need a random string should use this function unless they have a | ||
// very good reason to need a different amount of entropy. | ||
func Random256BitsString() string { | ||
// 32 bytes (256 bits) = 43 base64-encoded characters | ||
return RandomBitsString(256) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.