Best K6 code snippet using crypto.sha1
all_tests.go
Source:all_tests.go
...45 {"crypto/cipher/aead_test", "aes-256-gcm", "crypto/cipher/test/aes_256_gcm_tests.txt"},46 {"crypto/cipher/aead_test", "aes-256-key-wrap", "crypto/cipher/test/aes_256_key_wrap_tests.txt"},47 {"crypto/cipher/aead_test", "chacha20-poly1305", "crypto/cipher/test/chacha20_poly1305_tests.txt"},48 {"crypto/cipher/aead_test", "rc4-md5-tls", "crypto/cipher/test/rc4_md5_tls_tests.txt"},49 {"crypto/cipher/aead_test", "rc4-sha1-tls", "crypto/cipher/test/rc4_sha1_tls_tests.txt"},50 {"crypto/cipher/aead_test", "aes-128-cbc-sha1-tls", "crypto/cipher/test/aes_128_cbc_sha1_tls_tests.txt"},51 {"crypto/cipher/aead_test", "aes-128-cbc-sha1-tls-implicit-iv", "crypto/cipher/test/aes_128_cbc_sha1_tls_implicit_iv_tests.txt"},52 {"crypto/cipher/aead_test", "aes-128-cbc-sha256-tls", "crypto/cipher/test/aes_128_cbc_sha256_tls_tests.txt"},53 {"crypto/cipher/aead_test", "aes-256-cbc-sha1-tls", "crypto/cipher/test/aes_256_cbc_sha1_tls_tests.txt"},54 {"crypto/cipher/aead_test", "aes-256-cbc-sha1-tls-implicit-iv", "crypto/cipher/test/aes_256_cbc_sha1_tls_implicit_iv_tests.txt"},55 {"crypto/cipher/aead_test", "aes-256-cbc-sha256-tls", "crypto/cipher/test/aes_256_cbc_sha256_tls_tests.txt"},56 {"crypto/cipher/aead_test", "aes-256-cbc-sha384-tls", "crypto/cipher/test/aes_256_cbc_sha384_tls_tests.txt"},57 {"crypto/cipher/aead_test", "des-ede3-cbc-sha1-tls", "crypto/cipher/test/des_ede3_cbc_sha1_tls_tests.txt"},58 {"crypto/cipher/aead_test", "des-ede3-cbc-sha1-tls-implicit-iv", "crypto/cipher/test/des_ede3_cbc_sha1_tls_implicit_iv_tests.txt"},59 {"crypto/cipher/aead_test", "rc4-md5-ssl3", "crypto/cipher/test/rc4_md5_ssl3_tests.txt"},60 {"crypto/cipher/aead_test", "rc4-sha1-ssl3", "crypto/cipher/test/rc4_sha1_ssl3_tests.txt"},61 {"crypto/cipher/aead_test", "aes-128-cbc-sha1-ssl3", "crypto/cipher/test/aes_128_cbc_sha1_ssl3_tests.txt"},62 {"crypto/cipher/aead_test", "aes-256-cbc-sha1-ssl3", "crypto/cipher/test/aes_256_cbc_sha1_ssl3_tests.txt"},63 {"crypto/cipher/aead_test", "des-ede3-cbc-sha1-ssl3", "crypto/cipher/test/des_ede3_cbc_sha1_ssl3_tests.txt"},64 {"crypto/cipher/aead_test", "aes-128-ctr-hmac-sha256", "crypto/cipher/test/aes_128_ctr_hmac_sha256.txt"},65 {"crypto/cipher/aead_test", "aes-256-ctr-hmac-sha256", "crypto/cipher/test/aes_256_ctr_hmac_sha256.txt"},66 {"crypto/cipher/cipher_test", "crypto/cipher/test/cipher_test.txt"},67 {"crypto/cmac/cmac_test"},68 {"crypto/constant_time_test"},69 {"crypto/dh/dh_test"},70 {"crypto/digest/digest_test"},71 {"crypto/dsa/dsa_test"},72 {"crypto/ec/ec_test"},73 {"crypto/ec/example_mul"},74 {"crypto/ecdsa/ecdsa_test"},75 {"crypto/err/err_test"},76 {"crypto/evp/evp_extra_test"},77 {"crypto/evp/evp_test", "crypto/evp/evp_tests.txt"},...
prf.go
Source:prf.go
...5import (6 "crypto"7 "crypto/hmac"8 "crypto/md5"9 "crypto/sha1"10 "crypto/sha256"11 "crypto/sha512"12 "errors"13 "fmt"14 "hash"15)16// Split a premaster secret in two as specified in RFC 4346, section 5.17func splitPreMasterSecret(secret []byte) (s1, s2 []byte) {18 s1 = secret[0 : (len(secret)+1)/2]19 s2 = secret[len(secret)/2:]20 return21}22// pHash implements the P_hash function, as defined in RFC 4346, section 5.23func pHash(result, secret, seed []byte, hash func() hash.Hash) {24 h := hmac.New(hash, secret)25 h.Write(seed)26 a := h.Sum(nil)27 j := 028 for j < len(result) {29 h.Reset()30 h.Write(a)31 h.Write(seed)32 b := h.Sum(nil)33 copy(result[j:], b)34 j += len(b)35 h.Reset()36 h.Write(a)37 a = h.Sum(nil)38 }39}40// prf10 implements the TLS 1.0 pseudo-random function, as defined in RFC 2246, section 5.41func prf10(result, secret, label, seed []byte) {42 hashSHA1 := sha1.New43 hashMD5 := md5.New44 labelAndSeed := make([]byte, len(label)+len(seed))45 copy(labelAndSeed, label)46 copy(labelAndSeed[len(label):], seed)47 s1, s2 := splitPreMasterSecret(secret)48 pHash(result, s1, labelAndSeed, hashMD5)49 result2 := make([]byte, len(result))50 pHash(result2, s2, labelAndSeed, hashSHA1)51 for i, b := range result2 {52 result[i] ^= b53 }54}55// prf12 implements the TLS 1.2 pseudo-random function, as defined in RFC 5246, section 5.56func prf12(hashFunc func() hash.Hash) func(result, secret, label, seed []byte) {57 return func(result, secret, label, seed []byte) {58 labelAndSeed := make([]byte, len(label)+len(seed))59 copy(labelAndSeed, label)60 copy(labelAndSeed[len(label):], seed)61 pHash(result, secret, labelAndSeed, hashFunc)62 }63}64// prf30 implements the SSL 3.0 pseudo-random function, as defined in65// www.mozilla.org/projects/security/pki/nss/ssl/draft302.txt section 6.66func prf30(result, secret, label, seed []byte) {67 hashSHA1 := sha1.New()68 hashMD5 := md5.New()69 done := 070 i := 071 // RFC 5246 section 6.3 says that the largest PRF output needed is 12872 // bytes. Since no more ciphersuites will be added to SSLv3, this will73 // remain true. Each iteration gives us 16 bytes so 10 iterations will74 // be sufficient.75 var b [11]byte76 for done < len(result) {77 for j := 0; j <= i; j++ {78 b[j] = 'A' + byte(i)79 }80 hashSHA1.Reset()81 hashSHA1.Write(b[:i+1])82 hashSHA1.Write(secret)83 hashSHA1.Write(seed)84 digest := hashSHA1.Sum(nil)85 hashMD5.Reset()86 hashMD5.Write(secret)87 hashMD5.Write(digest)88 done += copy(result[done:], hashMD5.Sum(nil))89 i++90 }91}92const (93 tlsRandomLength = 32 // Length of a random nonce in TLS 1.1.94 masterSecretLength = 48 // Length of a master secret in TLS 1.1.95 finishedVerifyLength = 12 // Length of verify_data in a Finished message.96)97var masterSecretLabel = []byte("master secret")98var keyExpansionLabel = []byte("key expansion")99var clientFinishedLabel = []byte("client finished")100var serverFinishedLabel = []byte("server finished")101var extendedMasterSecretLabel = []byte("extended master secret")102func prfAndHashForVersion(version uint16, suite *cipherSuite) (func(result, secret, label, seed []byte), crypto.Hash) {103 switch version {104 case VersionSSL30:105 return prf30, crypto.Hash(0)106 case VersionTLS10, VersionTLS11:107 return prf10, crypto.Hash(0)108 case VersionTLS12:109 if suite.flags&suiteSHA384 != 0 {110 return prf12(sha512.New384), crypto.SHA384111 }112 return prf12(sha256.New), crypto.SHA256113 default:114 panic("unknown version")115 }116}117func prfForVersion(version uint16, suite *cipherSuite) func(result, secret, label, seed []byte) {118 prf, _ := prfAndHashForVersion(version, suite)119 return prf120}121// masterFromPreMasterSecret generates the master secret from the pre-master122// secret. See http://tools.ietf.org/html/rfc5246#section-8.1123func masterFromPreMasterSecret(version uint16, suite *cipherSuite, preMasterSecret, clientRandom, serverRandom []byte, fin finishedHash, ems bool) []byte {124 if ems {125 session_hash := fin.Sum()126 masterSecret := make([]byte, masterSecretLength)127 prfForVersion(version, suite)(masterSecret, preMasterSecret, extendedMasterSecretLabel, session_hash)128 return masterSecret129 } else {130 seed := make([]byte, 0, len(clientRandom)+len(serverRandom))131 seed = append(seed, clientRandom...)132 seed = append(seed, serverRandom...)133 masterSecret := make([]byte, masterSecretLength)134 prfForVersion(version, suite)(masterSecret, preMasterSecret, masterSecretLabel, seed)135 return masterSecret136 }137}138// keysFromMasterSecret generates the connection keys from the master139// secret, given the lengths of the MAC key, cipher key and IV, as defined in140// RFC 2246, section 6.3.141func keysFromMasterSecret(version uint16, suite *cipherSuite, masterSecret, clientRandom, serverRandom []byte, macLen, keyLen, ivLen int) (clientMAC, serverMAC, clientKey, serverKey, clientIV, serverIV []byte) {142 seed := make([]byte, 0, len(serverRandom)+len(clientRandom))143 seed = append(seed, serverRandom...)144 seed = append(seed, clientRandom...)145 n := 2*macLen + 2*keyLen + 2*ivLen146 keyMaterial := make([]byte, n)147 prfForVersion(version, suite)(keyMaterial, masterSecret, keyExpansionLabel, seed)148 clientMAC = keyMaterial[:macLen]149 keyMaterial = keyMaterial[macLen:]150 serverMAC = keyMaterial[:macLen]151 keyMaterial = keyMaterial[macLen:]152 clientKey = keyMaterial[:keyLen]153 keyMaterial = keyMaterial[keyLen:]154 serverKey = keyMaterial[:keyLen]155 keyMaterial = keyMaterial[keyLen:]156 clientIV = keyMaterial[:ivLen]157 keyMaterial = keyMaterial[ivLen:]158 serverIV = keyMaterial[:ivLen]159 return160}161// lookupTLSHash looks up the corresponding crypto.Hash for a given162// hash from a TLS SignatureScheme.163func lookupTLSHash(signatureAlgorithm SignatureScheme) (crypto.Hash, error) {164 switch signatureAlgorithm {165 case PKCS1WithSHA1, ECDSAWithSHA1:166 return crypto.SHA1, nil167 case PKCS1WithSHA256, PSSWithSHA256, ECDSAWithP256AndSHA256:168 return crypto.SHA256, nil169 case PKCS1WithSHA384, PSSWithSHA384, ECDSAWithP384AndSHA384:170 return crypto.SHA384, nil171 case PKCS1WithSHA512, PSSWithSHA512, ECDSAWithP521AndSHA512:172 return crypto.SHA512, nil173 default:174 return 0, fmt.Errorf("tls: unsupported signature algorithm: %#04x", signatureAlgorithm)175 }176}177func newFinishedHash(version uint16, cipherSuite *cipherSuite) finishedHash {178 var buffer []byte179 if version == VersionSSL30 || version >= VersionTLS12 {180 buffer = []byte{}181 }182 prf, hash := prfAndHashForVersion(version, cipherSuite)183 if hash != 0 {184 return finishedHash{hash.New(), hash.New(), nil, nil, buffer, version, prf}185 }186 return finishedHash{sha1.New(), sha1.New(), md5.New(), md5.New(), buffer, version, prf}187}188// A finishedHash calculates the hash of a set of handshake messages suitable189// for including in a Finished message.190type finishedHash struct {191 client hash.Hash192 server hash.Hash193 // Prior to TLS 1.2, an additional MD5 hash is required.194 clientMD5 hash.Hash195 serverMD5 hash.Hash196 // In TLS 1.2, a full buffer is sadly required.197 buffer []byte198 version uint16199 prf func(result, secret, label, seed []byte)200}201func (h *finishedHash) Write(msg []byte) (n int, err error) {202 h.client.Write(msg)203 h.server.Write(msg)204 if h.version < VersionTLS12 {205 h.clientMD5.Write(msg)206 h.serverMD5.Write(msg)207 }208 if h.buffer != nil {209 h.buffer = append(h.buffer, msg...)210 }211 return len(msg), nil212}213func (h finishedHash) Sum() []byte {214 if h.version >= VersionTLS12 {215 return h.client.Sum(nil)216 }217 out := make([]byte, 0, md5.Size+sha1.Size)218 out = h.clientMD5.Sum(out)219 return h.client.Sum(out)220}221// finishedSum30 calculates the contents of the verify_data member of a SSLv3222// Finished message given the MD5 and SHA1 hashes of a set of handshake223// messages.224func finishedSum30(md5, sha1 hash.Hash, masterSecret []byte, magic []byte) []byte {225 md5.Write(magic)226 md5.Write(masterSecret)227 md5.Write(ssl30Pad1[:])228 md5Digest := md5.Sum(nil)229 md5.Reset()230 md5.Write(masterSecret)231 md5.Write(ssl30Pad2[:])232 md5.Write(md5Digest)233 md5Digest = md5.Sum(nil)234 sha1.Write(magic)235 sha1.Write(masterSecret)236 sha1.Write(ssl30Pad1[:40])237 sha1Digest := sha1.Sum(nil)238 sha1.Reset()239 sha1.Write(masterSecret)240 sha1.Write(ssl30Pad2[:40])241 sha1.Write(sha1Digest)242 sha1Digest = sha1.Sum(nil)243 ret := make([]byte, len(md5Digest)+len(sha1Digest))244 copy(ret, md5Digest)245 copy(ret[len(md5Digest):], sha1Digest)246 return ret247}248var ssl3ClientFinishedMagic = [4]byte{0x43, 0x4c, 0x4e, 0x54}249var ssl3ServerFinishedMagic = [4]byte{0x53, 0x52, 0x56, 0x52}250// clientSum returns the contents of the verify_data member of a client's251// Finished message.252func (h finishedHash) clientSum(masterSecret []byte) []byte {253 if h.version == VersionSSL30 {254 return finishedSum30(h.clientMD5, h.client, masterSecret, ssl3ClientFinishedMagic[:])255 }256 out := make([]byte, finishedVerifyLength)257 h.prf(out, masterSecret, clientFinishedLabel, h.Sum())258 return out259}260// serverSum returns the contents of the verify_data member of a server's261// Finished message.262func (h finishedHash) serverSum(masterSecret []byte) []byte {263 if h.version == VersionSSL30 {264 return finishedSum30(h.serverMD5, h.server, masterSecret, ssl3ServerFinishedMagic[:])265 }266 out := make([]byte, finishedVerifyLength)267 h.prf(out, masterSecret, serverFinishedLabel, h.Sum())268 return out269}270// hashForClientCertificate returns a digest over the handshake messages so far,271// suitable for signing by a TLS client certificate.272func (h finishedHash) hashForClientCertificate(sigType uint8, hashAlg crypto.Hash, masterSecret []byte) ([]byte, error) {273 if (h.version == VersionSSL30 || h.version >= VersionTLS12) && h.buffer == nil {274 panic("a handshake hash for a client-certificate was requested after discarding the handshake buffer")275 }276 if h.version == VersionSSL30 {277 if sigType != signaturePKCS1v15 {278 return nil, errors.New("tls: unsupported signature type for client certificate")279 }280 md5Hash := md5.New()281 md5Hash.Write(h.buffer)282 sha1Hash := sha1.New()283 sha1Hash.Write(h.buffer)284 return finishedSum30(md5Hash, sha1Hash, masterSecret, nil), nil285 }286 if h.version >= VersionTLS12 {287 hash := hashAlg.New()288 hash.Write(h.buffer)289 return hash.Sum(nil), nil290 }291 if sigType == signatureECDSA {292 return h.server.Sum(nil), nil293 }294 return h.Sum(), nil295}296// discardHandshakeBuffer is called when there is no more need to297// buffer the entirety of the handshake messages.298func (h *finishedHash) discardHandshakeBuffer() {...
sha1
Using AI Code Generation
1import (2func main() {3 h := sha1.New()4 h.Write([]byte(s))5 bs := h.Sum(nil)6 fmt.Println(s)7 fmt.Printf("%x8}9import (10func main() {11 h := sha1.New()12 h.Write([]byte(s))13 bs := h.Sum(nil)14 fmt.Println(s)15 fmt.Printf("%x16}17import (18func main() {19 h := sha1.New()20 h.Write([]byte(s))21 bs := h.Sum(nil)22 fmt.Println(s)23 fmt.Printf("%x24}25import (26func main() {27 h := sha1.New()28 h.Write([]byte(s))29 bs := h.Sum(nil)30 fmt.Println(s)31 fmt.Printf("%x32}33import (34func main() {35 h := sha1.New()36 h.Write([]byte(s))
sha1
Using AI Code Generation
1import (2func main() {3 h := sha1.New()4 h.Write([]byte(s))5 bs := h.Sum(nil)6 fmt.Println(s)7 fmt.Printf("%x8}9import (10func main() {11 h := sha256.New()12 h.Write([]byte(s))13 bs := h.Sum(nil)14 fmt.Println(s)15 fmt.Printf("%x16}17import (18func main() {19 h := sha512.New()20 h.Write([]byte(s))21 bs := h.Sum(nil)22 fmt.Println(s)
sha1
Using AI Code Generation
1import (2func main() {3 h := sha1.New()4 h.Write([]byte(s))5 bs := h.Sum(nil)6 fmt.Println(s)7 fmt.Printf("%x8}
sha1
Using AI Code Generation
1import (2func main() {3 h := sha1.New()4 h.Write([]byte(s))5 bs := h.Sum(nil)6 fmt.Println(s)7 fmt.Printf("%x8}9import (10func main() {11 h := sha256.New()12 h.Write([]byte(s))
sha1
Using AI Code Generation
1import (2func main() {3 h := sha1.New()4 io.WriteString(h, "test")5 fmt.Printf("%x", h.Sum(nil))6}7import (8func main() {9 h := sha256.New()10 io.WriteString(h, "test")11 fmt.Printf("%x", h.Sum(nil))12}13import (14func main() {15 h := sha512.New()16 io.WriteString(h, "test")17 fmt.Printf("%x", h.Sum(nil))18}
sha1
Using AI Code Generation
1import (2func main() {3 file, err := os.Open("2.go")4 if err != nil {5 fmt.Println(err)6 }7 defer file.Close()8 hash := sha1.New()9 if _, err := io.Copy(hash, file); err != nil {10 fmt.Println(err)11 }12 fmt.Printf("%x", hash.Sum(nil))13}
sha1
Using AI Code Generation
1import (2func main() {3h := sha1.New()4io.WriteString(h, "test")5fmt.Printf("%x", h.Sum(nil))6}7import (8func main() {
sha1
Using AI Code Generation
1import (2func main() {3 h := sha1.New()4 h.Write([]byte("test"))5 bs := h.Sum(nil)6 fmt.Println(bs)7}8import (9func main() {10 h := sha256.New()11 h.Write([]byte("test"))12 bs := h.Sum(nil)13 fmt.Println(bs)14}15import (16func main() {17 h := sha512.New()18 h.Write([]byte("test"))19 bs := h.Sum(nil)20 fmt.Println(bs)21}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!