[daily] Go language Bible -- compound data type, array exercise

Keywords: Go

go language Bible - composite data type

1. Composite data types that can be constructed by combining basic types in different ways

2. Four types -- array, slice, map and structure

3. Array is composed of isomorphic elements - each array element is of the same type - structure is composed of heterogeneous elements

4.slice and map are dynamic data structures, which will grow dynamically as needed

 

go language Bible - array

1. Because the length of an array is fixed, arrays are rarely used directly in Go language

2. Array literal syntax initializes an array with a set of values

3. Array length is a part of array type

4. The% x adverb parameter of printf function is used to specify to print all elements of array or slice in hexadecimal format,% t adverb parameter is used to print Boolean data,% t adverb parameter is used to display the data type corresponding to a value

5. Function parameters can be explicitly passed in an array pointer, so that any modification of the array by the pointer can be directly fed back to the caller

Exercise 4.1: write a function to calculate the number of different bit s in two SHA256 hash codes. (refer to the PopCount function in section 2.6.2. )

 

Exercise 4.2: write a program to print the standard input SHA256 code by default, and support the command line flag customization to output the SHA384 or SHA512 hash algorithm.

 

1. View the following document: godoc crypto/sha256

func Sum256(data []byte) [Size]byte

2. godoc crypto

package main
import(
        "fmt"
        "crypto/sha256"
        "crypto/sha512"
        "flag"
        "strings"
)

//Command line flag
var hashMethod=flag.String("s","sha256","Please enter hash algorithm")
func main(){
        flag.Parse()
        num := compareSha256("x","X")
        fmt.Println(num)

        //Receive command line flag and judge
        printHash(strings.ToUpper(*hashMethod),"x")
}
/*
Exercise 4.1: write a function to calculate the number of different bit s in two SHA256 hash codes. (refer to the PopCount function in section 2.6.2. )
*/
func compareSha256(str1 string,str2 string)int{
        a := sha256.Sum256([]byte(str1))
        b := sha256.Sum256([]byte(str2))
        num := 0
        //Circular byte array
        for i:=0;i<len(a);i++{
                        //1 byte 8 bits, shift operation, get each bit
                        for m:=1;m<=8;m++{
                                //Compare whether each bit is the same
                                if (a[i] >> uint(m))!=(b[i] >>uint(m)){
                                        num++
                                }   
                        }   
        }   
        return num 
}
/*
Exercise 4.2: write a program to print the standard input SHA256 code by default, and support the command line flag customization to output the SHA384 or SHA512 hash algorithm.
*/
func printHash(flag string,str string){
        if flag=="SHA256"{
                fmt.Printf("%x\n",sha256.Sum256([]byte(str)))
                return
        }   
        if flag=="SHA512"{
                fmt.Printf("%x\n",sha512.Sum512([]byte(str)))
                return
        }   
        if flag=="SHA384"{
                fmt.Printf("%x\n",sha512.Sum384([]byte(str)))
                return
        }

}
  

  

Posted by jesirose on Fri, 03 Apr 2020 11:41:51 -0700