github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/x509/crypto.go (about) 1 package x509 2 3 /* 4 x509/crypto.go 重写Hash相关定义,添加SM3,用来代替`crypto.Hash` 5 */ 6 7 import ( 8 "crypto" 9 "errors" 10 "hash" 11 "strconv" 12 ) 13 14 // Hash 重写Hash相关定义,用来代替`crypto.Hash` 15 // Hash identifies a cryptographic hash function that is implemented in another 16 // package. 17 type Hash uint 18 19 // HashFunc simply returns the value of h so that Hash implements SignerOpts. 20 func (h Hash) HashFunc() crypto.Hash { 21 return crypto.Hash(h) 22 } 23 24 func (h Hash) String() string { 25 switch h { 26 case SM3: 27 return "SM3" 28 case MD4: 29 return "MD4" 30 case MD5: 31 return "MD5" 32 case SHA1: 33 return "SHA-1" 34 case SHA224: 35 return "SHA-224" 36 case SHA256: 37 return "SHA-256" 38 case SHA384: 39 return "SHA-384" 40 case SHA512: 41 return "SHA-512" 42 case MD5SHA1: 43 return "MD5+SHA1" 44 case RIPEMD160: 45 return "RIPEMD-160" 46 case SHA3_224: 47 return "SHA3-224" 48 case SHA3_256: 49 return "SHA3-256" 50 case SHA3_384: 51 return "SHA3-384" 52 case SHA3_512: 53 return "SHA3-512" 54 case SHA512_224: 55 return "SHA-512/224" 56 case SHA512_256: 57 return "SHA-512/256" 58 case BLAKE2s_256: 59 return "BLAKE2s-256" 60 case BLAKE2b_256: 61 return "BLAKE2b-256" 62 case BLAKE2b_384: 63 return "BLAKE2b-384" 64 case BLAKE2b_512: 65 return "BLAKE2b-512" 66 default: 67 return "unknown hash value " + strconv.Itoa(int(h)) 68 } 69 } 70 71 //goland:noinspection GoSnakeCaseUsage 72 const ( 73 MD4 Hash = 1 + iota // import golang.org/x/crypto/md4 74 MD5 // import crypto/md5 75 SHA1 // import crypto/sha1 76 SHA224 // import crypto/sha256 77 SHA256 // import crypto/sha256 78 SHA384 // import crypto/sha512 79 SHA512 // import crypto/sha512 80 MD5SHA1 // no implementation; MD5+SHA1 used for TLS RSA 81 RIPEMD160 // import golang.org/x/crypto/ripemd160 82 SHA3_224 // import golang.org/x/crypto/sha3 83 SHA3_256 // import golang.org/x/crypto/sha3 84 SHA3_384 // import golang.org/x/crypto/sha3 85 SHA3_512 // import golang.org/x/crypto/sha3 86 SHA512_224 // import crypto/sha512 87 SHA512_256 // import crypto/sha512 88 BLAKE2s_256 // import golang.org/x/crypto/blake2s 89 BLAKE2b_256 // import golang.org/x/crypto/blake2b 90 BLAKE2b_384 // import golang.org/x/crypto/blake2b 91 BLAKE2b_512 // import golang.org/x/crypto/blake2b 92 SM3 // 添加sm3 93 maxHash 94 ) 95 96 var digestSizes = []uint8{ 97 MD4: 16, 98 MD5: 16, 99 SHA1: 20, 100 SHA224: 28, 101 SHA256: 32, 102 SHA384: 48, 103 SHA512: 64, 104 SHA512_224: 28, 105 SHA512_256: 32, 106 SHA3_224: 28, 107 SHA3_256: 32, 108 SHA3_384: 48, 109 SHA3_512: 64, 110 MD5SHA1: 36, 111 RIPEMD160: 20, 112 BLAKE2s_256: 32, 113 BLAKE2b_256: 32, 114 BLAKE2b_384: 48, 115 BLAKE2b_512: 64, 116 SM3: 32, 117 } 118 119 // Size returns the length, in bytes, of a digest resulting from the given hash 120 // function. It doesn't require that the hash function in question be linked 121 // into the program. 122 func (h Hash) Size() int { 123 if h > 0 && h < maxHash { 124 return int(digestSizes[h]) 125 } 126 panic("crypto: Size of unknown hash function") 127 } 128 129 var hashes = make([]func() hash.Hash, maxHash) 130 131 // New returns a new hash.Hash calculating the given hash function. New panics 132 // if the hash function is not linked into the binary. 133 func (h Hash) New() hash.Hash { 134 if h > 0 && h < maxHash { 135 f := hashes[h] 136 if f != nil { 137 return f() 138 } 139 } 140 panic("crypto: requested hash function #" + strconv.Itoa(int(h)) + " is unavailable") 141 } 142 143 // Available reports whether the given hash function is linked into the binary. 144 func (h Hash) Available() bool { 145 return h < maxHash && hashes[h] != nil 146 } 147 148 // RegisterHash registers a function that returns a new instance of the given 149 // hash function. This is intended to be called from the init function in 150 // packages that implement hash functions. 151 func RegisterHash(h Hash, f func() hash.Hash) { 152 if h >= maxHash { 153 panic("crypto: RegisterHash of unknown hash function") 154 } 155 hashes[h] = f 156 } 157 158 //goland:noinspection GoUnusedExportedFunction 159 func Convert2CryptoHash(h Hash) (crypto.Hash, error) { 160 if h < SM3 { 161 return h.HashFunc(), nil 162 } else { 163 return crypto.Hash(maxHash), errors.New("crypto.Hash 不支持SM3") 164 } 165 } 166 167 // // PublicKey represents a public key using an unspecified algorithm. 168 // type PublicKey interface{} 169 170 // // PrivateKey represents a private key using an unspecified algorithm. 171 // type PrivateKey interface{} 172 173 // // Signer is an interface for an opaque private key that can be used for 174 // // signing operations. For example, an RSA key kept in a hardware module. 175 // type Signer interface { 176 // // Public returns the public key corresponding to the opaque, 177 // // private key. 178 // Public() PublicKey 179 180 // // Sign signs digest with the private key, possibly using entropy from 181 // // rand. For an RSA key, the resulting signature should be either a 182 // // PKCS #1 v1.5 or PSS signature (as indicated by opts). For an (EC)DSA 183 // // key, it should be a DER-serialised, ASN.1 signature structure. 184 // // 185 // // Hash implements the SignerOpts interface and, in most cases, one can 186 // // simply pass in the hash function used as opts. Sign may also attempt 187 // // to type assert opts to other types in order to obtain algorithm 188 // // specific values. See the documentation in each package for details. 189 // // 190 // // Note that when a signature of a hash of a larger message is needed, 191 // // the caller is responsible for hashing the larger message and passing 192 // // the hash (as digest) and the hash function (as opts) to Sign. 193 // Sign(rand io.Reader, digest []byte, opts SignerOpts) (signature []byte, err error) 194 // } 195 196 // // SignerOpts contains options for signing with a Signer. 197 // type SignerOpts interface { 198 // // HashFunc returns an identifier for the hash function used to produce 199 // // the message passed to Signer.Sign, or else zero to indicate that no 200 // // hashing was done. 201 // HashFunc() Hash 202 // } 203 204 // // Decrypter is an interface for an opaque private key that can be used for 205 // // asymmetric decryption operations. An example would be an RSA key 206 // // kept in a hardware module. 207 // type Decrypter interface { 208 // // Public returns the public key corresponding to the opaque, 209 // // private key. 210 // Public() PublicKey 211 212 // // Decrypt decrypts msg. The opts argument should be appropriate for 213 // // the primitive used. See the documentation in each implementation for 214 // // details. 215 // Decrypt(rand io.Reader, msg []byte, opts DecrypterOpts) (plaintext []byte, err error) 216 // } 217 218 // type DecrypterOpts interface{}