Best K6 code snippet using crypto.Digest
sha512.go
Source:sha512.go
1// Copyright 2009 The Go Authors. All rights reserved.2// Use of this source code is governed by a BSD-style3// license that can be found in the LICENSE file.4// Package sha512 implements the SHA-384, SHA-512, SHA-512/224, and SHA-512/2565// hash algorithms as defined in FIPS 180-4.6package sha5127import (8 "crypto"9 "hash"10)11func init() {12 crypto.RegisterHash(crypto.SHA384, New384)13 crypto.RegisterHash(crypto.SHA512, New)14 crypto.RegisterHash(crypto.SHA512_224, New512_224)15 crypto.RegisterHash(crypto.SHA512_256, New512_256)16}17const (18 // Size is the size, in bytes, of a SHA-512 checksum.19 Size = 6420 // Size224 is the size, in bytes, of a SHA-512/224 checksum.21 Size224 = 2822 // Size256 is the size, in bytes, of a SHA-512/256 checksum.23 Size256 = 3224 // Size384 is the size, in bytes, of a SHA-384 checksum.25 Size384 = 4826 // BlockSize is the block size, in bytes, of the SHA-512/224,27 // SHA-512/256, SHA-384 and SHA-512 hash functions.28 BlockSize = 12829)30const (31 chunk = 12832 init0 = 0x6a09e667f3bcc90833 init1 = 0xbb67ae8584caa73b34 init2 = 0x3c6ef372fe94f82b35 init3 = 0xa54ff53a5f1d36f136 init4 = 0x510e527fade682d137 init5 = 0x9b05688c2b3e6c1f38 init6 = 0x1f83d9abfb41bd6b39 init7 = 0x5be0cd19137e217940 init0_224 = 0x8c3d37c819544da241 init1_224 = 0x73e1996689dcd4d642 init2_224 = 0x1dfab7ae32ff9c8243 init3_224 = 0x679dd514582f9fcf44 init4_224 = 0x0f6d2b697bd44da845 init5_224 = 0x77e36f7304c4894246 init6_224 = 0x3f9d85a86a1d36c847 init7_224 = 0x1112e6ad91d692a148 init0_256 = 0x22312194fc2bf72c49 init1_256 = 0x9f555fa3c84c64c250 init2_256 = 0x2393b86b6f53b15151 init3_256 = 0x963877195940eabd52 init4_256 = 0x96283ee2a88effe353 init5_256 = 0xbe5e1e255386399254 init6_256 = 0x2b0199fc2c85b8aa55 init7_256 = 0x0eb72ddc81c52ca256 init0_384 = 0xcbbb9d5dc1059ed857 init1_384 = 0x629a292a367cd50758 init2_384 = 0x9159015a3070dd1759 init3_384 = 0x152fecd8f70e593960 init4_384 = 0x67332667ffc00b3161 init5_384 = 0x8eb44a876858151162 init6_384 = 0xdb0c2e0d64f98fa763 init7_384 = 0x47b5481dbefa4fa464)65// digest represents the partial evaluation of a checksum.66type digest struct {67 h [8]uint6468 x [chunk]byte69 nx int70 len uint6471 function crypto.Hash72}73func (d *digest) Reset() {74 switch d.function {75 case crypto.SHA384:76 d.h[0] = init0_38477 d.h[1] = init1_38478 d.h[2] = init2_38479 d.h[3] = init3_38480 d.h[4] = init4_38481 d.h[5] = init5_38482 d.h[6] = init6_38483 d.h[7] = init7_38484 case crypto.SHA512_224:85 d.h[0] = init0_22486 d.h[1] = init1_22487 d.h[2] = init2_22488 d.h[3] = init3_22489 d.h[4] = init4_22490 d.h[5] = init5_22491 d.h[6] = init6_22492 d.h[7] = init7_22493 case crypto.SHA512_256:94 d.h[0] = init0_25695 d.h[1] = init1_25696 d.h[2] = init2_25697 d.h[3] = init3_25698 d.h[4] = init4_25699 d.h[5] = init5_256100 d.h[6] = init6_256101 d.h[7] = init7_256102 default:103 d.h[0] = init0104 d.h[1] = init1105 d.h[2] = init2106 d.h[3] = init3107 d.h[4] = init4108 d.h[5] = init5109 d.h[6] = init6110 d.h[7] = init7111 }112 d.nx = 0113 d.len = 0114}115// New returns a new hash.Hash computing the SHA-512 checksum.116func New() hash.Hash {117 d := &digest{function: crypto.SHA512}118 d.Reset()119 return d120}121// New512_224 returns a new hash.Hash computing the SHA-512/224 checksum.122func New512_224() hash.Hash {123 d := &digest{function: crypto.SHA512_224}124 d.Reset()125 return d126}127// New512_256 returns a new hash.Hash computing the SHA-512/256 checksum.128func New512_256() hash.Hash {129 d := &digest{function: crypto.SHA512_256}130 d.Reset()131 return d132}133// New384 returns a new hash.Hash computing the SHA-384 checksum.134func New384() hash.Hash {135 d := &digest{function: crypto.SHA384}136 d.Reset()137 return d138}139func (d *digest) Size() int {140 switch d.function {141 case crypto.SHA512_224:142 return Size224143 case crypto.SHA512_256:144 return Size256145 case crypto.SHA384:146 return Size384147 default:148 return Size149 }150}151func (d *digest) BlockSize() int { return BlockSize }152func (d *digest) Write(p []byte) (nn int, err error) {153 nn = len(p)154 d.len += uint64(nn)155 if d.nx > 0 {156 n := copy(d.x[d.nx:], p)157 d.nx += n158 if d.nx == chunk {159 block(d, d.x[:])160 d.nx = 0161 }162 p = p[n:]163 }164 if len(p) >= chunk {165 n := len(p) &^ (chunk - 1)166 block(d, p[:n])167 p = p[n:]168 }169 if len(p) > 0 {170 d.nx = copy(d.x[:], p)171 }172 return173}174func (d0 *digest) Sum(in []byte) []byte {175 // Make a copy of d0 so that caller can keep writing and summing.176 d := new(digest)177 *d = *d0178 hash := d.checkSum()179 switch d.function {180 case crypto.SHA384:181 return append(in, hash[:Size384]...)182 case crypto.SHA512_224:183 return append(in, hash[:Size224]...)184 case crypto.SHA512_256:185 return append(in, hash[:Size256]...)186 default:187 return append(in, hash[:]...)188 }189}190func (d *digest) checkSum() [Size]byte {191 // Padding. Add a 1 bit and 0 bits until 112 bytes mod 128.192 len := d.len193 var tmp [128]byte194 tmp[0] = 0x80195 if len%128 < 112 {196 d.Write(tmp[0 : 112-len%128])197 } else {198 d.Write(tmp[0 : 128+112-len%128])199 }200 // Length in bits.201 len <<= 3202 for i := uint(0); i < 16; i++ {203 tmp[i] = byte(len >> (120 - 8*i))204 }205 d.Write(tmp[0:16])206 if d.nx != 0 {207 panic("d.nx != 0")208 }209 h := d.h[:]210 if d.function == crypto.SHA384 {211 h = d.h[:6]212 }213 var digest [Size]byte214 for i, s := range h {215 digest[i*8] = byte(s >> 56)216 digest[i*8+1] = byte(s >> 48)217 digest[i*8+2] = byte(s >> 40)218 digest[i*8+3] = byte(s >> 32)219 digest[i*8+4] = byte(s >> 24)220 digest[i*8+5] = byte(s >> 16)221 digest[i*8+6] = byte(s >> 8)222 digest[i*8+7] = byte(s)223 }224 return digest225}226// Sum512 returns the SHA512 checksum of the data.227func Sum512(data []byte) [Size]byte {228 d := digest{function: crypto.SHA512}229 d.Reset()230 d.Write(data)231 return d.checkSum()232}233// Sum384 returns the SHA384 checksum of the data.234func Sum384(data []byte) (sum384 [Size384]byte) {235 d := digest{function: crypto.SHA384}236 d.Reset()237 d.Write(data)238 sum := d.checkSum()239 copy(sum384[:], sum[:Size384])240 return241}242// Sum512_224 returns the Sum512/224 checksum of the data.243func Sum512_224(data []byte) (sum224 [Size224]byte) {244 d := digest{function: crypto.SHA512_224}245 d.Reset()246 d.Write(data)247 sum := d.checkSum()248 copy(sum224[:], sum[:Size224])249 return250}251// Sum512_256 returns the Sum512/256 checksum of the data.252func Sum512_256(data []byte) (sum256 [Size256]byte) {253 d := digest{function: crypto.SHA512_256}254 d.Reset()255 d.Write(data)256 sum := d.checkSum()257 copy(sum256[:], sum[:Size256])258 return259}...
kms-signer.go
Source:kms-signer.go
...15// KMSSigner is an interface for an opaque private key that can be used for16// signing operations. For example, an RSA key kept in a hardware module.17type KMSSigner interface {18 crypto.Signer19 Digest() crypto.Hash20}21// kmsSigner is a key22type kmsSigner struct {23 ctx context.Context24 client *cloudkms.KeyManagementClient25 keyName string26 publicKey crypto.PublicKey27 digest crypto.Hash28 forceDigest bool29}30// CryptoHashLookup maps crypto.hash to string name31var CryptoHashLookup = map[crypto.Hash]string{32 crypto.MD4: "MD4",33 crypto.SHA1: "SHA1",34 crypto.SHA224: "SHA224",35 crypto.SHA256: "SHA256",36 crypto.SHA384: "SHA384",37 crypto.SHA512: "SHA512",38 crypto.MD5SHA1: "MD5SHA1",39 crypto.RIPEMD160: "RIPEMD160",40 crypto.SHA3_224: "SHA3_224",41 crypto.SHA3_256: "SHA3_256",42 crypto.SHA3_384: "SHA3_384",43 crypto.SHA3_512: "SHA3_512",44 crypto.SHA512_224: "SHA512_224",45 crypto.SHA512_256: "SHA512_256",46 crypto.BLAKE2s_256: "BLAKE2s_256",47 crypto.BLAKE2b_256: "BLAKE2b_256",48 crypto.BLAKE2b_384: "BLAKE2b_384",49 crypto.BLAKE2b_512: "BLAKE2b_512",50}51// NewKMSSigner creates a new instance52func NewKMSSigner(keyName string, forceDigest bool) (signer KMSSigner, err error) {53 // Create the KMS client.54 ctx := context.Background()55 client, err := cloudkms.NewKeyManagementClient(ctx)56 if err != nil {57 log.Fatal(err)58 }59 // Retrieve the public key from KMS.60 response, err := client.GetPublicKey(ctx, &kmspb.GetPublicKeyRequest{Name: keyName})61 if err != nil {62 return nil, fmt.Errorf("GetPublicKey: %v", err)63 }64 // Parse the key.65 block, _ := pem.Decode([]byte(response.Pem))66 abstractKey, err := x509.ParsePKIXPublicKey(block.Bytes)67 if err != nil {68 return nil, fmt.Errorf("x509.ParsePKIXPublicKey: %+v", err)69 }70 var publicKey crypto.PublicKey71 var digestType crypto.Hash72 switch abstractKey.(type) {73 case *rsa.PublicKey:74 publicKey = abstractKey.(*rsa.PublicKey)75 keySize := publicKey.(*rsa.PublicKey).Size() * 876 switch keySize {77 case 2048:78 fallthrough79 case 3072:80 digestType = crypto.SHA25681 case 4096:82 digestType = crypto.SHA51283 default:84 return nil, fmt.Errorf("unsupported RSA key size %v", keySize)85 }86 case *ecdsa.PublicKey:87 publicKey = abstractKey.(*ecdsa.PublicKey)88 bitSize := publicKey.(*ecdsa.PublicKey).Curve.Params().BitSize89 switch bitSize {90 case 256:91 digestType = crypto.SHA25692 case 384:93 digestType = crypto.SHA38494 default:95 return nil, fmt.Errorf("unsupported ECDSA bit size %v", bitSize)96 }97 default:98 return nil, fmt.Errorf("key %q is not supported format", keyName)99 }100 return &kmsSigner{101 keyName: keyName,102 ctx: ctx,103 client: client,104 publicKey: publicKey,105 digest: digestType,106 forceDigest: forceDigest,107 }, nil108}109// Sign with key110func (kmss *kmsSigner) Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) (signature []byte, err error) {111 // Check opts to see if the digest algo matches112 if !kmss.forceDigest && opts.HashFunc() != kmss.digest {113 return nil, fmt.Errorf("Requested hash: %v, supported hash %v", CryptoHashLookup[opts.HashFunc()], CryptoHashLookup[kmss.digest])114 }115 // Build the request.116 var digestPayload kmspb.Digest117 switch kmss.digest {118 case crypto.SHA256:119 digestPayload = kmspb.Digest{120 Digest: &kmspb.Digest_Sha256{121 Sha256: digest,122 },123 }124 case crypto.SHA384:125 digestPayload = kmspb.Digest{126 Digest: &kmspb.Digest_Sha384{127 Sha384: digest,128 },129 }130 case crypto.SHA512:131 digestPayload = kmspb.Digest{132 Digest: &kmspb.Digest_Sha512{133 Sha512: digest,134 },135 }136 }137 req := &kmspb.AsymmetricSignRequest{138 Name: kmss.keyName,139 Digest: &digestPayload,140 }141 // Query the API.142 res, err := kmss.client.AsymmetricSign(kmss.ctx, req)143 if err != nil {144 return nil, fmt.Errorf("%v\nrequested hash: %v", err, CryptoHashLookup[opts.HashFunc()])145 }146 return res.Signature, nil147}148// Public fetches public key149func (kmss *kmsSigner) Public() crypto.PublicKey {150 return kmss.publicKey151}152// Digest returns hash algo used for this key153func (kmss *kmsSigner) Digest() crypto.Hash {154 return kmss.digest155}...
Digest
Using AI Code Generation
1import (2func main() {3 file, err := os.Open("1.go")4 if err != nil {5 fmt.Println(err)6 }7 defer file.Close()8 hash := md5.New()9 if _, err := io.Copy(hash, file); err != nil {10 fmt.Println(err)11 }12 hashInBytes := hash.Sum(nil)[:16]13 fmt.Printf("%x", hashInBytes)14}15import (16func main() {17 data := []byte("Hello World")18 fmt.Printf("%x", md5.Sum(data))19}20import (21func main() {22 file, err := os.Open("1.go")23 if err != nil {24 fmt.Println(err)25 }26 defer file.Close()27 hash := md5.New()28 if _, err := io.Copy(hash, file); err != nil {29 fmt.Println(err)30 }31 hashInBytes := hash.Sum(nil)[:16]32 fmt.Printf("%x", hashInBytes)33}34import (35func main() {36 data := []byte("Hello World")37 fmt.Printf("%x", md5.Sum(data))38}39import (40func main() {41 file, err := os.Open("1.go")
Digest
Using AI Code Generation
1import (2func main() {3 hash := sha1.New()4 hash.Write([]byte(input))5 sha1HashInBytes := hash.Sum(nil)6 sha1HashInString := hex.EncodeToString(sha1HashInBytes)7 fmt.Println("SHA1: ", sha1HashInString)8 hash = sha256.New()9 hash.Write([]byte(input))10 sha256HashInBytes := hash.Sum(nil)11 sha256HashInString := hex.EncodeToString(sha256HashInBytes)12 fmt.Println("SHA256: ", sha256HashInString)13 hash = sha512.New()14 hash.Write([]byte(input))15 sha512HashInBytes := hash.Sum(nil)16 sha512HashInString := hex.EncodeToString(sha512HashInBytes)17 fmt.Println("SHA512: ", sha512HashInString)18 hash = md5.New()19 hash.Write([]byte(input))20 md5HashInBytes := hash.Sum(nil)21 md5HashInString := hex.EncodeToString(md5HashInBytes)22 fmt.Println("MD5: ", md5HashInString)23 hash = sha1.New()24 hash.Write([]byte(input))25 sha1HashInBytes = hash.Sum(nil)26 sha1HashInString = hex.EncodeToString(sha1HashInBytes)27 fmt.Println("SHA1: ", sha1HashInString)28 hash = sha256.New()29 hash.Write([]byte(input))30 sha256HashInBytes = hash.Sum(nil)31 sha256HashInString = hex.EncodeToString(sha256HashInBytes)32 fmt.Println("SHA256: ", sha256HashInString)33 hash = sha512.New()34 hash.Write([]byte(input))35 sha512HashInBytes = hash.Sum(nil)
Digest
Using AI Code Generation
1import (2func main() {3h := sha256.New()4io.WriteString(h, str)5fmt.Printf("%x", h.Sum(nil))6}
Digest
Using AI Code Generation
1import (2func main() {3 file, err := os.Open("1.go")4 if err != nil {5 panic(err)6 }7 defer file.Close()8 hash := md5.New()9 if _, err := io.Copy(hash, file); err != nil {10 panic(err)11 }12 hashInBytes := hash.Sum(nil)[:16]
Digest
Using AI Code Generation
1import (2func main() {3 h := sha256.New()4 h.Write([]byte("Hello World!"))5 bs := h.Sum(nil)6 fmt.Println(bs)7}8import (9func main() {10 h := md5.New()11 h.Write([]byte("Hello World!"))12 bs := h.Sum(nil)13 fmt.Println(bs)14}15import (16func main() {17 h := sha1.New()18 h.Write([]byte("Hello World!"))19 bs := h.Sum(nil)20 fmt.Println(bs)21}
Digest
Using AI Code Generation
1import (2func main() {3 fmt.Println("Unhashed String: ", str)4 hash := md5.New()5 io.WriteString(hash, str)6 fmt.Println("Hashed String: ", hash.Sum(nil))7}8import (9func main() {10 fmt.Println("Unhashed String: ", str)11 hash := sha1.New()12 hash.Write([]byte(str))13 fmt.Println("Hashed String: ", hash.Sum(nil))14}
Digest
Using AI Code Generation
1import (2func main() {3 h := sha256.New()4 h.Write([]byte("Hello World"))5 bs := h.Sum(nil)6 fmt.Println(bs)7}8import (9func main() {10 h := sha256.New()11 bs := h.Sum([]byte("Hello World"))12 fmt.Println(bs)13}14import (15func main() {16 bs := sha256.Sum256([]byte("Hello World"))17 fmt.Println(bs)18}19import (20func main() {21 bs := sha256.Sum224([]byte("Hello World"))22 fmt.Println(bs)23}
Digest
Using AI Code Generation
1import (2func main() {3 data := []byte("Hello World")4 hash := md5.Sum(data)5 fmt.Printf("%x", hash)6}7import (8func main() {9 data := []byte("Hello World")10 hash := md5.Sum(data)11 fmt.Printf("%x12 str := hex.EncodeToString(hash[:])13 fmt.Println(str)14}15import (16func main() {17 data := []byte("Hello World")18 hash := md5.Sum(data)19 fmt.Printf("%x20 str := hex.EncodeToString(hash[:])21 fmt.Println(str)
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!!