hash.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package hash
  14. import (
  15. "crypto/sha256"
  16. "encoding/json"
  17. "fmt"
  18. "k8s.io/api/core/v1"
  19. )
  20. // ConfigMapHash returns a hash of the ConfigMap.
  21. // The Data, Kind, and Name are taken into account.
  22. func ConfigMapHash(cm *v1.ConfigMap) (string, error) {
  23. encoded, err := encodeConfigMap(cm)
  24. if err != nil {
  25. return "", err
  26. }
  27. h, err := encodeHash(hash(encoded))
  28. if err != nil {
  29. return "", err
  30. }
  31. return h, nil
  32. }
  33. // SecretHash returns a hash of the Secret.
  34. // The Data, Kind, Name, and Type are taken into account.
  35. func SecretHash(sec *v1.Secret) (string, error) {
  36. encoded, err := encodeSecret(sec)
  37. if err != nil {
  38. return "", err
  39. }
  40. h, err := encodeHash(hash(encoded))
  41. if err != nil {
  42. return "", err
  43. }
  44. return h, nil
  45. }
  46. // encodeConfigMap encodes a ConfigMap.
  47. // Data, Kind, and Name are taken into account.
  48. func encodeConfigMap(cm *v1.ConfigMap) (string, error) {
  49. // json.Marshal sorts the keys in a stable order in the encoding
  50. m := map[string]interface{}{"kind": "ConfigMap", "name": cm.Name, "data": cm.Data}
  51. if len(cm.BinaryData) > 0 {
  52. m["binaryData"] = cm.BinaryData
  53. }
  54. data, err := json.Marshal(m)
  55. if err != nil {
  56. return "", err
  57. }
  58. return string(data), nil
  59. }
  60. // encodeSecret encodes a Secret.
  61. // Data, Kind, Name, and Type are taken into account.
  62. func encodeSecret(sec *v1.Secret) (string, error) {
  63. // json.Marshal sorts the keys in a stable order in the encoding
  64. data, err := json.Marshal(map[string]interface{}{"kind": "Secret", "type": sec.Type, "name": sec.Name, "data": sec.Data})
  65. if err != nil {
  66. return "", err
  67. }
  68. return string(data), nil
  69. }
  70. // encodeHash extracts the first 40 bits of the hash from the hex string
  71. // (1 hex char represents 4 bits), and then maps vowels and vowel-like hex
  72. // characters to consonants to prevent bad words from being formed (the theory
  73. // is that no vowels makes it really hard to make bad words). Since the string
  74. // is hex, the only vowels it can contain are 'a' and 'e'.
  75. // We picked some arbitrary consonants to map to from the same character set as GenerateName.
  76. // See: https://github.com/kubernetes/apimachinery/blob/dc1f89aff9a7509782bde3b68824c8043a3e58cc/pkg/util/rand/rand.go#L75
  77. // If the hex string contains fewer than ten characters, returns an error.
  78. func encodeHash(hex string) (string, error) {
  79. if len(hex) < 10 {
  80. return "", fmt.Errorf("the hex string must contain at least 10 characters")
  81. }
  82. enc := []rune(hex[:10])
  83. for i := range enc {
  84. switch enc[i] {
  85. case '0':
  86. enc[i] = 'g'
  87. case '1':
  88. enc[i] = 'h'
  89. case '3':
  90. enc[i] = 'k'
  91. case 'a':
  92. enc[i] = 'm'
  93. case 'e':
  94. enc[i] = 't'
  95. }
  96. }
  97. return string(enc), nil
  98. }
  99. // hash hashes `data` with sha256 and returns the hex string
  100. func hash(data string) string {
  101. return fmt.Sprintf("%x", sha256.Sum256([]byte(data)))
  102. }