How to use sha256 method of crypto Package

Best K6 code snippet using crypto.sha256

default_crypto.go

Source:default_crypto.go Github

copy

Full Screen

...4 "crypto/aes"5 "crypto/cipher"6 "crypto/hmac"7 "crypto/rand"8 "crypto/sha256"9 "fmt"10 "io"11 "golang.org/x/crypto/curve25519"12 "golang.org/x/crypto/hkdf"13)14// DefaultCrypto is an implementation of Crypto with cryptographic primitives recommended15// by the Double Ratchet Algorithm specification. However, some details are different,16// see function comments for details.17type DefaultCrypto struct{}18// See the Crypto interface.19func (c DefaultCrypto) GenerateDH() (DHPair, error) {20 var privKey [32]byte21 if _, err := io.ReadFull(rand.Reader, privKey[:]); err != nil {22 return dhPair{}, fmt.Errorf("couldn't generate privKey: %s", err)23 }24 privKey[0] &= 24825 privKey[31] &= 12726 privKey[31] |= 6427 var pubKey [32]byte28 curve25519.ScalarBaseMult(&pubKey, &privKey)29 return dhPair{30 privateKey: privKey,31 publicKey: pubKey,32 }, nil33}34// See the Crypto interface.35func (c DefaultCrypto) DH(dhPair DHPair, dhPub Key) Key {36 var (37 dhOut [32]byte38 privKey [32]byte = dhPair.PrivateKey()39 pubKey [32]byte = dhPub40 )41 curve25519.ScalarMult(&dhOut, &privKey, &pubKey)42 return dhOut43}44// See the Crypto interface.45func (c DefaultCrypto) KdfRK(rk, dhOut Key) (rootKey, chainKey, headerKey Key) {46 var (47 r = hkdf.New(sha256.New, dhOut[:], rk[:], []byte("rsZUpEuXUqqwXBvSy3EcievAh4cMj6QL"))48 buf = make([]byte, 96)49 )50 // The only error here is an entropy limit which won't be reached for such a short buffer.51 _, _ = io.ReadFull(r, buf)52 copy(rootKey[:], buf[:32])53 copy(chainKey[:], buf[32:64])54 copy(headerKey[:], buf[64:96])55 return56}57// See the Crypto interface.58func (c DefaultCrypto) KdfCK(ck Key) (chainKey Key, msgKey Key) {59 const (60 ckInput = 1561 mkInput = 1662 )63 h := hmac.New(sha256.New, ck[:])64 h.Write([]byte{ckInput})65 copy(chainKey[:], h.Sum(nil))66 h.Reset()67 h.Write([]byte{mkInput})68 copy(msgKey[:], h.Sum(nil))69 return chainKey, msgKey70}71// Encrypt uses a slightly different approach than in the algorithm specification:72// it uses AES-256-CTR instead of AES-256-CBC for security, ciphertext length and implementation73// complexity considerations.74func (c DefaultCrypto) Encrypt(mk Key, plaintext, ad []byte) []byte {75 encKey, authKey, iv := c.deriveEncKeys(mk)76 ciphertext := make([]byte, aes.BlockSize+len(plaintext))77 copy(ciphertext, iv[:])78 var (79 block, _ = aes.NewCipher(encKey[:]) // No error will occur here as encKey is guaranteed to be 32 bytes.80 stream = cipher.NewCTR(block, iv[:])81 )82 stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)83 return append(ciphertext, c.computeSignature(authKey[:], ciphertext, ad)...)84}85// See the Crypto interface.86func (c DefaultCrypto) Decrypt(mk Key, authCiphertext, ad []byte) ([]byte, error) {87 var (88 l = len(authCiphertext)89 ciphertext = authCiphertext[:l-sha256.Size]90 signature = authCiphertext[l-sha256.Size:]91 )92 // Check the signature.93 encKey, authKey, _ := c.deriveEncKeys(mk)94 if s := c.computeSignature(authKey[:], ciphertext, ad); !bytes.Equal(s, signature) {95 return nil, fmt.Errorf("invalid signature")96 }97 // Decrypt.98 var (99 block, _ = aes.NewCipher(encKey[:]) // No error will occur here as encKey is guaranteed to be 32 bytes.100 stream = cipher.NewCTR(block, ciphertext[:aes.BlockSize])101 plaintext = make([]byte, len(ciphertext[aes.BlockSize:]))102 )103 stream.XORKeyStream(plaintext, ciphertext[aes.BlockSize:])104 return plaintext, nil105}106// deriveEncKeys derive keys for message encryption and decryption. Returns (encKey, authKey, iv, err).107func (c DefaultCrypto) deriveEncKeys(mk Key) (encKey Key, authKey Key, iv [16]byte) {108 // First, derive encryption and authentication key out of mk.109 salt := make([]byte, 32)110 var (111 r = hkdf.New(sha256.New, mk[:], salt, []byte("pcwSByyx2CRdryCffXJwy7xgVZWtW5Sh"))112 buf = make([]byte, 80)113 )114 // The only error here is an entropy limit which won't be reached for such a short buffer.115 _, _ = io.ReadFull(r, buf)116 copy(encKey[:], buf[0:32])117 copy(authKey[:], buf[32:64])118 copy(iv[:], buf[64:80])119 return120}121func (c DefaultCrypto) computeSignature(authKey, ciphertext, associatedData []byte) []byte {122 h := hmac.New(sha256.New, authKey)123 h.Write(associatedData)124 h.Write(ciphertext)125 return h.Sum(nil)126}127type dhPair struct {128 privateKey Key129 publicKey Key130}131func (p dhPair) PrivateKey() Key {132 return p.privateKey133}134func (p dhPair) PublicKey() Key {135 return p.publicKey136}...

Full Screen

Full Screen

params.go

Source:params.go Github

copy

Full Screen

...33 "crypto"34 "crypto/aes"35 "crypto/cipher"36 "crypto/elliptic"37 "crypto/sha256"38 "crypto/sha512"39 "fmt"40 "hash"41 ethcrypto "github.com/ethereum/go-ethereum/crypto"42)43var (44 DefaultCurve = ethcrypto.S256()45 ErrUnsupportedECDHAlgorithm = fmt.Errorf("ecies: unsupported ECDH algorithm")46 ErrUnsupportedECIESParameters = fmt.Errorf("ecies: unsupported ECIES parameters")47 ErrInvalidKeyLen = fmt.Errorf("ecies: invalid key size (> %d) in ECIESParams", maxKeyLen)48)49// KeyLen is limited to prevent overflow of the counter50// in concatKDF. While the theoretical limit is much higher,51// no known cipher uses keys larger than 512 bytes.52const maxKeyLen = 51253type ECIESParams struct {54 Hash func() hash.Hash // hash function55 hashAlgo crypto.Hash56 Cipher func([]byte) (cipher.Block, error) // symmetric cipher57 BlockSize int // block size of symmetric cipher58 KeyLen int // length of symmetric key59}60// Standard ECIES parameters:61// * ECIES using AES128 and HMAC-SHA-256-1662// * ECIES using AES256 and HMAC-SHA-256-3263// * ECIES using AES256 and HMAC-SHA-384-4864// * ECIES using AES256 and HMAC-SHA-512-6465var (66 ECIES_AES128_SHA256 = &ECIESParams{67 Hash: sha256.New,68 hashAlgo: crypto.SHA256,69 Cipher: aes.NewCipher,70 BlockSize: aes.BlockSize,71 KeyLen: 16,72 }73 ECIES_AES256_SHA256 = &ECIESParams{74 Hash: sha256.New,75 hashAlgo: crypto.SHA256,76 Cipher: aes.NewCipher,77 BlockSize: aes.BlockSize,78 KeyLen: 32,79 }80 ECIES_AES256_SHA384 = &ECIESParams{81 Hash: sha512.New384,82 hashAlgo: crypto.SHA384,83 Cipher: aes.NewCipher,84 BlockSize: aes.BlockSize,85 KeyLen: 32,86 }87 ECIES_AES256_SHA512 = &ECIESParams{88 Hash: sha512.New,...

Full Screen

Full Screen

server_proof.go

Source:server_proof.go Github

copy

Full Screen

...3 "crypto"4 "crypto/ecdsa"5 "crypto/rand"6 "crypto/rsa"7 "crypto/sha256"8 "crypto/tls"9 "crypto/x509"10 "encoding/asn1"11 "errors"12 "math/big"13)14type ecdsaSignature struct {15 R, S *big.Int16}17// signServerProof signs CHLO and server config for use in the server proof18func signServerProof(cert *tls.Certificate, chlo []byte, serverConfigData []byte) ([]byte, error) {19 hash := sha256.New()20 hash.Write([]byte("QUIC CHLO and server config signature\x00"))21 chloHash := sha256.Sum256(chlo)22 hash.Write([]byte{32, 0, 0, 0})23 hash.Write(chloHash[:])24 hash.Write(serverConfigData)25 key, ok := cert.PrivateKey.(crypto.Signer)26 if !ok {27 return nil, errors.New("expected PrivateKey to implement crypto.Signer")28 }29 opts := crypto.SignerOpts(crypto.SHA256)30 if _, ok = key.(*rsa.PrivateKey); ok {31 opts = &rsa.PSSOptions{SaltLength: 32, Hash: crypto.SHA256}32 }33 return key.Sign(rand.Reader, hash.Sum(nil), opts)34}35// verifyServerProof verifies the server proof signature36func verifyServerProof(proof []byte, cert *x509.Certificate, chlo []byte, serverConfigData []byte) bool {37 hash := sha256.New()38 hash.Write([]byte("QUIC CHLO and server config signature\x00"))39 chloHash := sha256.Sum256(chlo)40 hash.Write([]byte{32, 0, 0, 0})41 hash.Write(chloHash[:])42 hash.Write(serverConfigData)43 // RSA44 if cert.PublicKeyAlgorithm == x509.RSA {45 opts := &rsa.PSSOptions{SaltLength: 32, Hash: crypto.SHA256}46 err := rsa.VerifyPSS(cert.PublicKey.(*rsa.PublicKey), crypto.SHA256, hash.Sum(nil), proof, opts)47 return err == nil48 }49 // ECDSA50 signature := &ecdsaSignature{}51 rest, err := asn1.Unmarshal(proof, signature)52 if err != nil || len(rest) != 0 {53 return false...

Full Screen

Full Screen

sha256

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 c1 := sha256.Sum256([]byte("x"))4 c2 := sha256.Sum256([]byte("X"))5 fmt.Printf("%x6}7import (8func main() {9 c1 := sha512.Sum512([]byte("x"))10 c2 := sha512.Sum512([]byte("X"))11 fmt.Printf("%x12}13import (14func main() {15 c1 := sha512.Sum512([]byte("x"))16 c2 := sha512.Sum512([]byte("X"))17 fmt.Printf("%x18}19import (20func main() {21 c1 := sha512.Sum512([]byte("x"))22 c2 := sha512.Sum512([]byte("X"))23 fmt.Printf("%x24}25import (26func main() {27 c1 := sha512.Sum512([]byte("x"))28 c2 := sha512.Sum512([]byte("X"))29 fmt.Printf("%x30}31import (

Full Screen

Full Screen

sha256

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 c1 := sha256.Sum256([]byte("x"))4 c2 := sha256.Sum256([]byte("X"))5 fmt.Printf("%x6}7import (8func main() {9 c1 := sha256.Sum256([]byte("x"))10 c2 := sha256.Sum256([]byte("X"))11 fmt.Printf("%x12}13import (14func main() {

Full Screen

Full Screen

sha256

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 c1 := sha256.Sum256([]byte("x"))4 c2 := sha256.Sum256([]byte("X"))5 fmt.Printf("%x6}7import (8func main() {9 c1 := sha512.Sum512([]byte("x"))10 c2 := sha512.Sum512([]byte("X"))11 fmt.Printf("%x12}

Full Screen

Full Screen

sha256

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 c1 := sha256.Sum256([]byte("x"))4 c2 := sha256.Sum256([]byte("X"))5 fmt.Printf("%x6}7import (8func main() {9 c1 := sha256.Sum256([]byte("x"))10 c2 := sha256.Sum256([]byte("X"))11 fmt.Printf("%x12 fmt.Println(countDiffBits(c1, c2))13}14func countDiffBits(c1, c2 [32]byte) int {15 for i := 0; i < len(c1); i++ {16 for j := 0; j < 8; j++ {17 if c1[i]&(1<<j) != c2[i]&(1<<j) {18 }19 }20 }21}

Full Screen

Full Screen

sha256

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Hello, playground")4 c1 := sha256.Sum256([]byte("x"))5 c2 := sha256.Sum256([]byte("X"))6 fmt.Printf("%x7}8import (9func main() {10 fmt.Println("Hello, playground")11 c1 := sha256.Sum256([]byte("x"))12 c2 := sha256.Sum256([]byte("X"))13 fmt.Printf("%x14}15import (16func main() {17 fmt.Println("Hello, playground")18 c1 := sha256.Sum256([]byte("x"))19 c2 := sha256.Sum256([]byte("X"))20 fmt.Printf("%x21}22import (23func main() {24 fmt.Println("Hello, playground")25 c1 := sha256.Sum256([]byte("x"))26 c2 := sha256.Sum256([]byte("X"))27 fmt.Printf("%x28}29import (30func main() {31 fmt.Println("Hello, playground")32 c1 := sha256.Sum256([]byte("x"))33 c2 := sha256.Sum256([]byte("X"))34 fmt.Printf("%x

Full Screen

Full Screen

sha256

Using AI Code Generation

copy

Full Screen

1import(2func main() {3 hasher := sha256.New()4 hasher.Write([]byte("Hello World"))5 result := hasher.Sum(nil)6 fmt.Printf("%x", result)7}

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful