How to use New method of crypto Package

Best K6 code snippet using crypto.New

trie_node_test.go

Source:trie_node_test.go Github

copy

Full Screen

1/*2Licensed to the Apache Software Foundation (ASF) under one3or more contributor license agreements. See the NOTICE file4distributed with this work for additional information5regarding copyright ownership. The ASF licenses this file6to you under the Apache License, Version 2.0 (the7"License"); you may not use this file except in compliance8with the License. You may obtain a copy of the License at9 http://www.apache.org/licenses/LICENSE-2.010Unless required by applicable law or agreed to in writing,11software distributed under the License is distributed on an12"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13KIND, either express or implied. See the License for the14specific language governing permissions and limitations15under the License.16*/17package trie18import (19 "github.com/openblockchain/obc-peer/openchain/ledger/testutil"20 "testing"21)22func TestTrieNode_MarshalUnmarshal_NoValue_NoChildren(t *testing.T) {23 testTrieNodeMarshalUnmarshal(24 newTrieNode(newTrieKey("chaincodeID", "key"),25 []byte{},26 false),27 t)28}29func TestTrieNode_MarshalUnmarshal_WithValue(t *testing.T) {30 testTrieNodeMarshalUnmarshal(31 newTrieNode(newTrieKey("chaincodeID", "key"),32 []byte("Hello!"),33 false),34 t)35}36func TestTrieNode_MarshalUnmarshal_WithChildren(t *testing.T) {37 trieNode := newTrieNode(newTrieKey("chaincodeID", "key"), []byte("Hello!"), false)38 trieNode.setChildCryptoHash(0, []byte("crypto-hash-for-test-0"))39 trieNode.setChildCryptoHash(15, []byte("crypto-hash-for-test-15"))40 testTrieNodeMarshalUnmarshal(trieNode, t)41}42func TestTrieNode_MergeAttributes(t *testing.T) {43 trieNode := newTrieNode(newTrieKey("chaincodeID", "key"), []byte("newValue!"), true)44 trieNode.setChildCryptoHash(0, []byte("crypto-hash-for-test-0"))45 trieNode.setChildCryptoHash(5, []byte("crypto-hash-for-test-5"))46 existingTrieNode := newTrieNode(newTrieKey("chaincodeID", "key"), []byte("existingValue"), false)47 existingTrieNode.setChildCryptoHash(5, []byte("crypto-hash-for-test-5-existing"))48 existingTrieNode.setChildCryptoHash(10, []byte("crypto-hash-for-test-10-existing"))49 trieNode.mergeMissingAttributesFrom(existingTrieNode)50 testutil.AssertEquals(t, trieNode.value, []byte("newValue!"))51 testutil.AssertEquals(t, trieNode.childrenCryptoHashes[0], []byte("crypto-hash-for-test-0"))52 testutil.AssertEquals(t, trieNode.childrenCryptoHashes[5], []byte("crypto-hash-for-test-5"))53 testutil.AssertEquals(t, trieNode.childrenCryptoHashes[10], []byte("crypto-hash-for-test-10-existing"))54}55func TestTrieNode_ComputeCryptoHash_NoValue_NoChild(t *testing.T) {56 trieNode := newTrieNode(newTrieKey("chaincodeID", "key"), nil, false)57 hash := trieNode.computeCryptoHash()58 testutil.AssertEquals(t, hash, nil)59}60func TestTrieNode_ComputeCryptoHash_NoValue_SingleChild(t *testing.T) {61 trieNode := newTrieNode(newTrieKey("chaincodeID", "key"), nil, false)62 singleChildCryptoHash := []byte("childCryptoHash-0")63 trieNode.setChildCryptoHash(0, singleChildCryptoHash)64 hash := trieNode.computeCryptoHash()65 testutil.AssertEquals(t, hash, singleChildCryptoHash)66}67func TestTrieNode_ComputeCryptoHash_NoValue_ManyChildren(t *testing.T) {68 trieKey := newTrieKey("chaincodeID", "key")69 child_0_CryptoHash := []byte("childCryptoHash-0")70 child_5_CryptoHash := []byte("childCryptoHash-5")71 child_15_CryptoHash := []byte("childCryptoHash-15")72 trieNode := newTrieNode(trieKey, nil, false)73 trieNode.setChildCryptoHash(0, child_0_CryptoHash)74 trieNode.setChildCryptoHash(5, child_5_CryptoHash)75 trieNode.setChildCryptoHash(15, child_15_CryptoHash)76 hash := trieNode.computeCryptoHash()77 expectedHashContent := computeTestHash(child_0_CryptoHash, child_5_CryptoHash, child_15_CryptoHash)78 testutil.AssertEquals(t, hash, expectedHashContent)79}80func TestTrieNode_ComputeCryptoHash_WithValue_NoChild(t *testing.T) {81 trieKey := newTrieKey("chaincodeID", "key")82 value := []byte("testValue")83 trieNode := newTrieNode(trieKey, value, false)84 hash := trieNode.computeCryptoHash()85 expectedHash := computeTestHash(trieKey.getEncodedBytes(), value)86 testutil.AssertEquals(t, hash, expectedHash)87}88func TestTrieNode_ComputeCryptoHash_WithValue_SingleChild(t *testing.T) {89 trieKey := newTrieKey("chaincodeID", "key")90 value := []byte("testValue")91 child_0_CryptoHash := []byte("childCryptoHash-0")92 trieNode := newTrieNode(trieKey, value, false)93 trieNode.setChildCryptoHash(0, child_0_CryptoHash)94 hash := trieNode.computeCryptoHash()95 expectedHash := computeTestHash(trieKey.getEncodedBytes(), value, child_0_CryptoHash)96 testutil.AssertEquals(t, hash, expectedHash)97}98func TestTrieNode_ComputeCryptoHash_WithValue_ManyChildren(t *testing.T) {99 trieKey := newTrieKey("chaincodeID", "key")100 value := []byte("testValue")101 child_0_CryptoHash := []byte("childCryptoHash-0")102 child_5_CryptoHash := []byte("childCryptoHash-5")103 child_15_CryptoHash := []byte("childCryptoHash-15")104 trieNode := newTrieNode(trieKey, value, false)105 trieNode.setChildCryptoHash(0, child_0_CryptoHash)106 trieNode.setChildCryptoHash(5, child_5_CryptoHash)107 trieNode.setChildCryptoHash(15, child_15_CryptoHash)108 hash := trieNode.computeCryptoHash()109 expectedHash := computeTestHash(trieKey.getEncodedBytes(), value, child_0_CryptoHash, child_5_CryptoHash, child_15_CryptoHash)110 testutil.AssertEquals(t, hash, expectedHash)111}112func testTrieNodeMarshalUnmarshal(trieNode *trieNode, t *testing.T) {113 trieNodeTestWrapper := &trieNodeTestWrapper{trieNode, t}114 serializedContent := trieNodeTestWrapper.marshal()115 trieNodeFromUnmarshal := trieNodeTestWrapper.unmarshal(trieNode.trieKey, serializedContent)116 testutil.AssertEquals(t, trieNodeFromUnmarshal.trieKey, trieNode.trieKey)117 testutil.AssertEquals(t, trieNodeFromUnmarshal.value, trieNode.value)118 testutil.AssertEquals(t, trieNodeFromUnmarshal.childrenCryptoHashes, trieNode.childrenCryptoHashes)119 testutil.AssertEquals(t, trieNodeFromUnmarshal.getNumChildren(), trieNode.getNumChildren())120}...

Full Screen

Full Screen

bucket_node_test.go

Source:bucket_node_test.go Github

copy

Full Screen

1/*2Licensed to the Apache Software Foundation (ASF) under one3or more contributor license agreements. See the NOTICE file4distributed with this work for additional information5regarding copyright ownership. The ASF licenses this file6to you under the Apache License, Version 2.0 (the7"License"); you may not use this file except in compliance8with the License. You may obtain a copy of the License at9 http://www.apache.org/licenses/LICENSE-2.010Unless required by applicable law or agreed to in writing,11software distributed under the License is distributed on an12"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13KIND, either express or implied. See the License for the14specific language governing permissions and limitations15under the License.16*/17package buckettree18import (19 "github.com/openblockchain/obc-peer/openchain/ledger/testutil"20 "testing"21)22func TestBucketNodeComputeHash(t *testing.T) {23 conf = initConfig(26, 3, fnvHash)24 bucketNode := newBucketNode(newBucketKey(2, 7))25 testutil.AssertEquals(t, bucketNode.computeCryptoHash(), nil)26 childKey1 := newBucketKey(3, 19)27 bucketNode.setChildCryptoHash(childKey1, []byte("cryptoHashChild1"))28 testutil.AssertEquals(t, bucketNode.computeCryptoHash(), []byte("cryptoHashChild1"))29 childKey3 := newBucketKey(3, 21)30 bucketNode.setChildCryptoHash(childKey3, []byte("cryptoHashChild3"))31 testutil.AssertEquals(t, bucketNode.computeCryptoHash(), testutil.ComputeCryptoHash([]byte("cryptoHashChild1cryptoHashChild3")))32 childKey2 := newBucketKey(3, 20)33 bucketNode.setChildCryptoHash(childKey2, []byte("cryptoHashChild2"))34 testutil.AssertEquals(t, bucketNode.computeCryptoHash(), testutil.ComputeCryptoHash([]byte("cryptoHashChild1cryptoHashChild2cryptoHashChild3")))35}36func TestBucketNodeMerge(t *testing.T) {37 conf = initConfig(26, 3, fnvHash)38 bucketNode := newBucketNode(newBucketKey(2, 7))39 bucketNode.childrenCryptoHash[0] = []byte("cryptoHashChild1")40 bucketNode.childrenCryptoHash[2] = []byte("cryptoHashChild3")41 dbBucketNode := newBucketNode(newBucketKey(2, 7))42 dbBucketNode.childrenCryptoHash[0] = []byte("DBcryptoHashChild1")43 dbBucketNode.childrenCryptoHash[1] = []byte("DBcryptoHashChild2")44 bucketNode.mergeBucketNode(dbBucketNode)45 testutil.AssertEquals(t, bucketNode.childrenCryptoHash[0], []byte("cryptoHashChild1"))46 testutil.AssertEquals(t, bucketNode.childrenCryptoHash[1], []byte("DBcryptoHashChild2"))47 testutil.AssertEquals(t, bucketNode.childrenCryptoHash[2], []byte("cryptoHashChild3"))48}49func TestBucketNodeMarshalUnmarshal(t *testing.T) {50 conf = initConfig(26, 3, fnvHash)51 bucketNode := newBucketNode(newBucketKey(2, 7))52 childKey1 := newBucketKey(3, 19)53 bucketNode.setChildCryptoHash(childKey1, []byte("cryptoHashChild1"))54 childKey3 := newBucketKey(3, 21)55 bucketNode.setChildCryptoHash(childKey3, []byte("cryptoHashChild3"))56 serializedBytes := bucketNode.marshal()57 deserializedBucketNode := unmarshalBucketNode(newBucketKey(2, 7), serializedBytes)58 testutil.AssertEquals(t, bucketNode.bucketKey, deserializedBucketNode.bucketKey)59 testutil.AssertEquals(t, bucketNode.childrenCryptoHash, deserializedBucketNode.childrenCryptoHash)60}...

Full Screen

Full Screen

New

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 key := []byte("example key 1234")4 plaintext := []byte("exampleplaintext")5 c, err := aes.NewCipher(key)6 if err != nil {7 fmt.Println(err)8 }9 gcm, err := cipher.NewGCM(c)10 if err != nil {11 fmt.Println(err)12 }13 nonce := make([]byte, gcm.NonceSize())14 if _, err = io.ReadFull(rand.Reader, nonce); err != nil {15 fmt.Println(err)16 }17 ciphertext := gcm.Seal(nonce, nonce, plaintext, nil)18 fmt.Printf("%x19}20import (21func main() {22 key := []byte("example key 1234")23 ciphertext, _ := hex.DecodeString("f3e1da9d9e9e06f5cd651af28e39d353436170746578616d706c657074657874")24 c, err := aes.NewCipher(key)25 if err != nil {26 fmt.Println(err)27 }28 gcm, err := cipher.NewGCM(c)29 if err != nil {30 fmt.Println(err)31 }32 nonceSize := gcm.NonceSize()33 plaintext, err := gcm.Open(nil, nonce, ciphertext, nil)34 if err != nil {35 fmt.Println(err)36 }37 fmt.Printf("%s38}

Full Screen

Full Screen

New

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 b := make([]byte, 20)4 _, err := rand.Read(b)5 if err != nil {6 fmt.Println("Error:", err)

Full Screen

Full Screen

New

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 b := make([]byte, 5)4 n, err := rand.Read(b)5 if err != nil {6 fmt.Println("error:", err)7 }8 fmt.Println(n, "bytes:", b)9}10import (11func main() {12 max := big.NewInt(1000)13 i, err := rand.Int(rand.Reader, max)14 if err != nil {15 fmt.Println("error:", err)16 }17 fmt.Println(i)18}19import (20func main() {21 max := big.NewInt(1000)22 i, err := rand.Int(rand.Reader, max)23 if err != nil {24 fmt.Println("error:", err)25 }26 rand.Seed(i.Int64())27 fmt.Println(rand.Intn(1000))28}29import (30func main() {31 b := make([]byte, 5)32 n, err := rand.Read(b)33 if err != nil {34 fmt.Println("error:", err)35 }36 fmt.Println(n, "bytes:", b)37}38import (39func main() {40 b := make([]byte, 5)41 n, err := rand.Read(b)42 if err != nil {43 fmt.Println("error:", err)44 }45 fmt.Println(n, "bytes:", b)46}

Full Screen

Full Screen

New

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 hash := sha256.New224()4 fmt.Printf("%T5}6import (7func main() {8 hash := sha256.New()9 fmt.Printf("%T10 hash.Write([]byte("Hello World!"))11}12import (13func main() {14 hash := sha256.New()15 fmt.Printf("%T16 hash.Write([]byte("Hello World!"))17 fmt.Printf("%x18", hash.Sum(nil))19}

Full Screen

Full Screen

New

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 hash := crypto.MD5.New()4 fmt.Println(hash)5}6import (7func main() {8 hash := md5.New()9 fmt.Println(hash)10}11import (12func main() {13 data := []byte("Hello Golang")14 hash := md5.Sum(data)15 fmt.Println(hash)16}17import (18func main() {19 data := []byte("Hello Golang")20 hash := md5.Sum(data)21 fmt.Printf("%x22}23import (24func main() {25 data := []byte("Hello Golang")26 hash := md5.Sum(data)27 fmt.Printf("%x28}29import (30func main() {31 data := []byte("Hello Golang")32 hash := md5.New()

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