How to use TestHMac method of crypto Package

Best K6 code snippet using crypto.TestHMac

hdwallet_encryption.go

Source:hdwallet_encryption.go Github

copy

Full Screen

1package cnlib2import (3 "bytes"4 "crypto/aes"5 "crypto/cipher"6 "crypto/hmac"7 "crypto/rand"8 "crypto/sha256"9 "crypto/sha512"10 "errors"11 "github.com/btcsuite/btcd/btcec"12)13const minPayloadSize = 13114// decrypt data using public/private keypair15func decrypt(data []byte, privateKey *btcec.PrivateKey) ([]byte, error) {16 if len(data) < minPayloadSize {17 return nil, errors.New("insufficient data")18 }19 version := data[:1]20 options := data[1:2]21 iv := data[2:18]22 cipherText := data[18:(len(data) - 32 - 65)]23 hmacVal := data[len(data)-32-65 : len(data)-65]24 publicKeyUncomp := data[len(data)-65:]25 if options[0] != byte(0) {26 return nil, errors.New("invalid payload option")27 }28 msg := make([]byte, 0)29 msg = append(msg, version...)30 msg = append(msg, options...)31 msg = append(msg, iv...)32 msg = append(msg, cipherText...)33 publicKey, err := btcec.ParsePubKey(publicKeyUncomp, btcec.S256())34 if err != nil {35 return nil, err36 }37 secret := generateSharedSecretRFC4753(privateKey, publicKey)38 keyData := sha512.Sum512(secret)39 encKey := keyData[:32]40 hmacKey := keyData[32:]41 testHmac := hmac.New(sha256.New, hmacKey)42 _, err = testHmac.Write(msg)43 if err != nil {44 return nil, errors.New("failed to write testHmac")45 }46 testHmacVal := testHmac.Sum(nil)47 // its important to use hmac.Equal to not leak time48 // information. See https://github.com/RNCryptor/RNCryptor-Spec49 if verified := hmac.Equal(testHmacVal, hmacVal); !verified {50 return nil, errors.New("invalid hmac")51 }52 cipherBlock, err := aes.NewCipher(encKey)53 if err != nil {54 return nil, err55 }56 decrypted := make([]byte, len(cipherText))57 copy(decrypted, cipherText)58 decrypter := cipher.NewCBCDecrypter(cipherBlock, iv)59 decrypter.CryptBlocks(decrypted, decrypted)60 // un-padd decrypted data61 length := len(decrypted)62 unpadding := int(decrypted[length-1])63 return decrypted[:(length - unpadding)], nil64}65// encrypt Data using public/private keypair66func encrypt(data []byte, privateKey *btcec.PrivateKey, publicKey *btcec.PublicKey) ([]byte, error) {67 secret := generateSharedSecretRFC4753(privateKey, publicKey)68 keyData := sha512.Sum512(secret)69 encKey := keyData[:32]70 hmacKey := keyData[32:]71 iv, err := randBytes(16)72 if err != nil {73 return nil, err74 }75 cipherText := make([]byte, len(data))76 copy(cipherText, data)77 version := byte(3)78 options := byte(0) // No Password, No HMAC Salt, No Enryption Salt79 msg := make([]byte, 0)80 msg = append(msg, version)81 msg = append(msg, options)82 msg = append(msg, iv...)83 cipherBlock, err := aes.NewCipher(encKey)84 if err != nil {85 return nil, err86 }87 // padd text for encryption88 blockSize := cipherBlock.BlockSize()89 padding := blockSize - len(cipherText)%blockSize90 padText := bytes.Repeat([]byte{byte(padding)}, padding)91 cipherText = append(cipherText, padText...)92 encrypter := cipher.NewCBCEncrypter(cipherBlock, iv)93 encrypter.CryptBlocks(cipherText, cipherText)94 msg = append(msg, cipherText...)95 hmacSrc := hmac.New(sha256.New, hmacKey)96 _, err = hmacSrc.Write(msg)97 if err != nil {98 return nil, errors.New("failed to write hmacSrc")99 }100 hmacVal := hmacSrc.Sum(nil)101 msg = append(msg, hmacVal...)102 msg = append(msg, privateKey.PubKey().SerializeUncompressed()...)103 return msg, nil104}105func randBytes(num int64) ([]byte, error) {106 bits := make([]byte, num)107 _, err := rand.Read(bits)108 if err != nil {109 return nil, err110 }111 return bits, nil112}113// generateSharedSecretRFC4753 generates the shared secret by multiplying the public and private key and114// combining the X and Y component to get the shared secret. This is the old way of generating a shared115// secret used by libbitcoin116func generateSharedSecretRFC4753(privkey *btcec.PrivateKey, pubkey *btcec.PublicKey) []byte {117 x, y := btcec.S256().ScalarMult(pubkey.X, pubkey.Y, privkey.D.Bytes())118 sharedSecretPublicKey := btcec.PublicKey{Curve: btcec.S256(), X: x, Y: y}119 return sharedSecretPublicKey.SerializeUncompressed()120}...

Full Screen

Full Screen

rncryptor.go

Source:rncryptor.go Github

copy

Full Screen

1package rncryptor2import(3 "bytes"4 "errors"5 "crypto/rand"6 "crypto/sha1"7 "crypto/sha256"8 "crypto/hmac"9 "crypto/aes"10 "crypto/cipher"11 "golang.org/x/crypto/pbkdf2"12)13func Decrypt(password string, data []byte) ([]byte, error) {14 version := data[:1]15 options := data[1:2]16 encSalt := data[2:10]17 hmacSalt := data[10:18]18 iv := data[18:34]19 cipherText := data[34:(len(data)-66+34)]20 expectedHmac := data[len(data)-32:len(data)]21 msg := make([]byte, 0)22 msg = append(msg, version...)23 msg = append(msg, options...)24 msg = append(msg, encSalt...)25 msg = append(msg, hmacSalt...)26 msg = append(msg, iv...)27 msg = append(msg, cipherText...)28 hmacKey := pbkdf2.Key([]byte(password), hmacSalt, 10000, 32, sha1.New)29 testHmac := hmac.New(sha256.New, hmacKey)30 testHmac.Write(msg)31 testHmacVal := testHmac.Sum(nil)32 // its important to use hmac.Equal to not leak time33 // information. See https://github.com/RNCryptor/RNCryptor-Spec34 verified := hmac.Equal(testHmacVal, expectedHmac)35 if !verified {36 return nil, errors.New("Password may be incorrect, or the data has been corrupted. (HMAC could not be verified)")37 }38 cipherKey := pbkdf2.Key([]byte(password), encSalt, 10000, 32, sha1.New)39 cipherBlock, err := aes.NewCipher(cipherKey)40 if err != nil {41 return nil, err42 }43 decrypted := make([]byte, len(cipherText))44 copy(decrypted, cipherText)45 decrypter := cipher.NewCBCDecrypter(cipherBlock, iv)46 decrypter.CryptBlocks(decrypted, decrypted)47 // un-padd decrypted data48 length := len(decrypted)49 unpadding := int(decrypted[length-1])50 return decrypted[:(length - unpadding)], nil51}52func Encrypt(password string, data []byte) ([]byte, error) {53 encSalt, encSaltErr := RandBytes(8)54 if encSaltErr != nil {55 return nil, encSaltErr56 }57 hmacSalt, hmacSaltErr := RandBytes(8)58 if hmacSaltErr != nil {59 return nil, hmacSaltErr60 }61 iv, ivErr := RandBytes(16)62 if ivErr != nil {63 return nil, ivErr64 }65 encrypted, encErr := EncryptWithOptions(password, data, encSalt, hmacSalt, iv)66 if encErr != nil {67 return nil, encErr68 }69 return encrypted, nil70}71func EncryptWithOptions(password string, data, encSalt, hmacSalt, iv []byte) ([]byte, error) {72 if len(password) < 1 {73 return nil, errors.New("Password cannot be empty")74 }75 encKey := pbkdf2.Key([]byte(password), encSalt, 10000, 32, sha1.New)76 hmacKey := pbkdf2.Key([]byte(password), hmacSalt, 10000, 32, sha1.New)77 cipherText := make([]byte, len(data))78 copy(cipherText, data)79 version := byte(3)80 options := byte(1)81 msg := make([]byte, 0)82 msg = append(msg, version)83 msg = append(msg, options)84 msg = append(msg, encSalt...)85 msg = append(msg, hmacSalt...)86 msg = append(msg, iv...)87 cipherBlock, cipherBlockErr := aes.NewCipher(encKey)88 if cipherBlockErr != nil {89 return nil, cipherBlockErr90 }91 // padd text for encryption92 blockSize := cipherBlock.BlockSize()93 padding := blockSize - len(cipherText)%blockSize94 padText := bytes.Repeat([]byte{byte(padding)}, padding)95 cipherText = append(cipherText, padText...)96 encrypter := cipher.NewCBCEncrypter(cipherBlock, iv)97 encrypter.CryptBlocks(cipherText, cipherText)98 msg = append(msg, cipherText...)99 hmacSrc := hmac.New(sha256.New, hmacKey)100 hmacSrc.Write(msg)101 hmacVal := hmacSrc.Sum(nil)102 msg = append(msg, hmacVal...)103 return msg, nil104}105func RandBytes(num int64) ([]byte, error) {106 bits := make([]byte, num)107 _, err := rand.Read(bits)108 if err != nil {109 return nil, err110 }111 return bits, nil112}...

Full Screen

Full Screen

tester.go

Source:tester.go Github

copy

Full Screen

1package main2import "projects/crypto"3func main() {4 crypto.TestHmac()5 crypto.TestSha256()6 crypto.TestMd5()7}...

Full Screen

Full Screen

TestHMac

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("HMAC using sha256")4 key := []byte("12345678901234567890123456789012")5 data := []byte("Hello World")6 mac := hmac.New(sha256.New, key)7 mac.Write(data)8 expectedMAC := mac.Sum(nil)9 fmt.Println("HMAC:", hex.EncodeToString(expectedMAC))10}11import (12func main() {13 fmt.Println("HMAC using sha512")14 key := []byte("12345678901234567890123456789012")15 data := []byte("Hello World")16 mac := hmac.New(sha512.New, key)17 mac.Write(data)18 expectedMAC := mac.Sum(nil)19 fmt.Println("HMAC:", hex.EncodeToString(expectedMAC))20}21import (22func main() {23 fmt.Println("HMAC using md5")24 key := []byte("12345678901234567890123456789012")25 data := []byte("Hello World

Full Screen

Full Screen

TestHMac

Using AI Code Generation

copy

Full Screen

1import "crypto"2import "crypto/hmac"3import "encoding/hex"4import "fmt"5import "io"6import "os"7func main() {8 h := crypto.SHA1.New()9 h1 := crypto.SHA256.New()10 h2 := crypto.SHA512.New()11 h3 := crypto.MD5.New()12 h4 := crypto.SHA1.New()13 h5 := crypto.SHA256.New()14 h6 := crypto.SHA512.New()15 h7 := crypto.MD5.New()16 h8 := crypto.SHA1.New()17 h9 := crypto.SHA256.New()18 h10 := crypto.SHA512.New()19 h11 := crypto.MD5.New()20 h12 := crypto.SHA1.New()21 h13 := crypto.SHA256.New()22 h14 := crypto.SHA512.New()23 h15 := crypto.MD5.New()24 h16 := crypto.SHA1.New()25 h17 := crypto.SHA256.New()26 h18 := crypto.SHA512.New()

Full Screen

Full Screen

TestHMac

Using AI Code Generation

copy

Full Screen

1import (2func main() {3}4func testHmac() {5 h := hmac.New(sha256.New, []byte("key"))6 h.Write([]byte("Hello World"))

Full Screen

Full Screen

TestHMac

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Hello World")4 cryptoObj.TestHMac()5}6import (7type Crypto struct {8}9func (cryptoObj Crypto) TestHMac() {10 h := hmac.New(sha256.New, []byte("key"))11 h.Write([]byte("Hello World"))12 fmt.Printf("%x13", h.Sum(nil))14}15import (16func TestHMac(t *testing.T) {17 cryptoObj.TestHMac()18}

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