Exemplo n.º 1
0
// Convert ECC-256 Public Key to an EMP address (raw 25 bytes).
func GetAddress(log chan string, x, y *big.Int) []byte {
	pubKey := elliptic.Marshal(elliptic.P256(), x, y)
	ripemd := ripemd160.New()

	sum := sha512.Sum384(pubKey)
	sumslice := make([]byte, sha512.Size384, sha512.Size384)
	for i := 0; i < sha512.Size384; i++ {
		sumslice[i] = sum[i]
	}

	ripemd.Write(sumslice)
	appender := ripemd.Sum(nil)
	appender = appender[len(appender)-20:]
	address := make([]byte, 1, 1)

	// Version 0x01
	address[0] = 0x01
	address = append(address, appender...)

	sum = sha512.Sum384(address)
	sum = sha512.Sum384(sum[:])

	for i := 0; i < 4; i++ {
		address = append(address, sum[i])
	}

	return address
}
Exemplo n.º 2
0
func _passpharseHash(passpharse []byte, encodingWay []string) (encodedpasspharse []byte) {
	encodepasspharse := passpharse
	for _, hashalgor := range encodingWay {
		switch hashalgor {
		case "md5":
			sum := md5.Sum(encodepasspharse)
			encodepasspharse = sum[:]
		case "sha1":
			sum := sha1.Sum(encodepasspharse)
			encodepasspharse = sum[:]
		case "sha224":
			sum := sha256.Sum224(encodepasspharse)
			encodepasspharse = sum[:]
		case "sha256":
			sum := sha256.Sum256(encodepasspharse)
			encodepasspharse = sum[:]
		case "sha384":
			sum := sha512.Sum384(encodepasspharse)
			encodepasspharse = sum[:]
		case "sha512":
			sum := sha512.Sum512(encodepasspharse)
			encodepasspharse = sum[:]
		}
	}
	//issue if return with not args,the return value will be null
	return encodepasspharse
}
Exemplo n.º 3
0
func main() {
	flag.Parse()
	if len(flag.Args()) != 0 {
		flagutil.UsageErrorf("unknown arguments: %v", flag.Args())
	}

	written := make(map[[sha512.Size384]byte]struct{})

	var skipped uint64
	rd := delimited.NewReader(os.Stdin)
	wr := delimited.NewWriter(os.Stdout)
	for {
		rec, err := rd.Next()
		if err == io.EOF {
			break
		} else if err != nil {
			log.Fatal(err)
		}

		hash := sha512.Sum384(rec)
		if _, ok := written[hash]; ok {
			skipped++
			continue
		}
		if err := wr.Put(rec); err != nil {
			log.Fatal(err)
		}
		written[hash] = struct{}{}
	}
	log.Printf("dedup_stream: skipped %d records", skipped)
}
Exemplo n.º 4
0
// Add Purge Token to database, and remove corresponding message if necessary.
func AddPurge(log chan string, p objects.Purge) error {
	mutex.Lock()
	defer mutex.Unlock()

	txid := p.GetBytes()
	hashArr := sha512.Sum384(txid)
	hash := hashArr[:]

	if hashList == nil || dbConn == nil {
		return DBError(EUNINIT)
	}
	hashObj := new(objects.Hash)
	hashObj.FromBytes(hash)

	if Contains(*hashObj) == PURGE {
		return nil
	}

	err := dbConn.Exec("INSERT INTO purge VALUES (?, ?)", hash, txid)
	if err != nil {
		log <- fmt.Sprintf("Error inserting purge into db... %s", err)
		return err
	}

	Add(*hashObj, PURGE)
	return nil
}
Exemplo n.º 5
0
func main() {
	length := len(os.Args)
	if length > 2 {
		if os.Args[2] == "-384" {
			c1 := sha512.Sum384([]byte(os.Args[1]))
			fmt.Printf("%s\n%x\n%T\n", os.Args[1], c1, c1)
		} else if os.Args[2] == "-512" {
			c1 := sha512.Sum512([]byte(os.Args[1]))
			fmt.Printf("%s\n%x\n%T\n", os.Args[1], c1, c1)
		} else {
			c1 := sha256.Sum256([]byte(os.Args[1]))
			fmt.Printf("%s\n%x\n%T\n", os.Args[1], c1, c1)
		}
	} else if length == 2 {
		c1 := sha256.Sum256([]byte(os.Args[1]))
		fmt.Printf("%s\n%x\n%T\n", os.Args[1], c1, c1)
	} else {
		fmt.Printf("less arguments")
	}
	// Output:
	// 2d711642b726b04401627ca9fbac32f5c8530fb1903cc4db02258717921a4881
	// 4b68ab3847feda7d6c62c1fbcbeebfa35eab7351ed5e78f4ddadea5df64b8015
	// false
	// [32]uint8
}
Exemplo n.º 6
0
// BaseLayerID returns the 64 byte hex ID for the baselayer name.
func (r *RootFS) BaseLayerID() string {
	if r.Type != TypeLayersWithBase {
		panic("tried to get base layer ID without a base layer")
	}
	baseID := sha512.Sum384([]byte(r.BaseLayer))
	return fmt.Sprintf("%x", baseID[:32])
}
Exemplo n.º 7
0
// Determine if address is valid (checksum is correct, correct length, and it starts with a 1-byte number).
func ValidateAddress(addr []byte) bool {
	if len(addr) != 25 {
		return false
	}
	ripe := addr[:21]
	sum := sha512.Sum384(ripe)
	sum = sha512.Sum384(sum[:])

	for i := 0; i < 4; i++ {
		if sum[i] != addr[i+21] {
			return false
		}
	}

	return true
}
Exemplo n.º 8
0
func main() {
	usage := "Usage: echo -n hello | ./ex02 [256|384|512]"
	bytes, err := ioutil.ReadAll(os.Stdin)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	n := 256
	if len(os.Args) == 2 {
		n, _ = strconv.Atoi(os.Args[1])
	}

	switch n {
	case 256:
		fmt.Printf("%x\n", sha256.Sum256(bytes))
		os.Exit(0)
		return
	case 384:
		fmt.Printf("%x\n", sha512.Sum384(bytes))
		os.Exit(0)
		return
	case 512:
		fmt.Printf("%x\n", sha512.Sum512(bytes))
		os.Exit(0)
		return
	default:
		fmt.Println(usage)
		os.Exit(1)
		return
	}
}
Exemplo n.º 9
0
func (c *CLI) Run(args []string) int {
	flagSet := flag.NewFlagSet(args[0], flag.ExitOnError)
	flagSet.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage of %s:\n", args[0])
		flagSet.PrintDefaults()
	}
	hashType := flagSet.String("type", "sha256", "Hash algorithm (sha256, sha384 or sha512)")

	if err := flagSet.Parse(args[1:]); err != nil {
		return ExitCodeParseFlagError
	}

	bytes, err := ioutil.ReadAll(c.inStream)
	if err != nil {
		return ReadStdinError
	}

	switch *hashType {
	case "sha256":
		fmt.Fprintf(c.outStream, "%x\n", sha256.Sum256(bytes))
	case "sha384":
		fmt.Fprintf(c.outStream, "%x\n", sha512.Sum384(bytes))
	case "sha512":
		fmt.Fprintf(c.outStream, "%x\n", sha512.Sum512(bytes))
	default:
		flagSet.Usage()
		return ExitCodeParseFlagError
	}
	return ExitCodeOK
}
Exemplo n.º 10
0
func verify(pub *ecdsa.PublicKey, data, sig []byte) bool {
	var unpackedSig ECDSASignature
	_, err := asn1.Unmarshal(sig, &unpackedSig)
	if err != nil {
		return false
	}
	h := sha512.Sum384(data)
	return ecdsa.Verify(pub, h[:], unpackedSig.R, unpackedSig.S)
}
Exemplo n.º 11
0
// 返回值:长度120
func _createpasswd(salt, passwd []byte) string {
	var orig []byte

	orig = append(salt, passwd...)
	orig = append(orig, _secret...)
	hsh := sha512.Sum384(orig)

	return hex.EncodeToString(append(salt, hsh[:]...))
}
Exemplo n.º 12
0
// GetCustomImageInfos returns the image infos for window specific
// base images which should always be present.
func (d *Driver) GetCustomImageInfos() ([]CustomImageInfo, error) {
	strData, err := hcsshim.GetSharedBaseImages()
	if err != nil {
		return nil, fmt.Errorf("Failed to restore base images: %s", err)
	}

	type customImageInfoList struct {
		Images []CustomImageInfo
	}

	var infoData customImageInfoList

	if err = json.Unmarshal([]byte(strData), &infoData); err != nil {
		err = fmt.Errorf("JSON unmarshal returned error=%s", err)
		logrus.Error(err)
		return nil, err
	}

	var images []CustomImageInfo

	for _, imageData := range infoData.Images {
		folderName := filepath.Base(imageData.Path)

		// Use crypto hash of the foldername to generate a docker style id.
		h := sha512.Sum384([]byte(folderName))
		id := fmt.Sprintf("%x", h[:32])

		if err := d.Create(id, "", "", nil); err != nil {
			return nil, err
		}
		// Create the alternate ID file.
		if err := d.setID(id, folderName); err != nil {
			return nil, err
		}

		imageData.ID = id

		// For now, hard code that all base images except nanoserver depend on win32k support
		if imageData.Name != "NanoServer" {
			imageData.OSFeatures = append(imageData.OSFeatures, "win32k")
		}

		versionData := strings.Split(imageData.Version, ".")
		if len(versionData) != 4 {
			logrus.Warn("Could not parse Windows version %s", imageData.Version)
		} else {
			// Include just major.minor.build, skip the fourth version field, which does not influence
			// OS compatibility.
			imageData.OSVersion = strings.Join(versionData[:3], ".")
		}

		images = append(images, imageData)
	}

	return images, nil
}
Exemplo n.º 13
0
func hashBySHA(s string, size int) {
	switch size {
	case 384:
		fmt.Printf("%x\n", sha512.Sum384([]byte(s)))
	case 512:
		fmt.Printf("%x\n", sha512.Sum512([]byte(s)))
	default:
		fmt.Printf("%x\n", sha256.Sum256([]byte(s)))
	}
}
Exemplo n.º 14
0
func sign(priv *ecdsa.PrivateKey, data []byte) ([]byte, error) {
	var sig ECDSASignature
	var err error

	h := sha512.Sum384(data)
	sig.R, sig.S, err = ecdsa.Sign(prng, priv, h[:])
	if err != nil {
		return nil, err
	}
	return asn1.Marshal(sig)
}
Exemplo n.º 15
0
func main() {

	flag.Parse()

	if *t {
		fmt.Printf("SHA384 = %x\n", sha512.Sum384([]byte(os.Args[1])))
	} else if *f {
		fmt.Printf("SHA512 = %x\n", sha512.Sum512([]byte(os.Args[1])))
	} else {
		fmt.Printf("SHA256 = %x\n", sha256.Sum256([]byte(os.Args[1])))
	}
}
Exemplo n.º 16
0
Arquivo: ex42.go Projeto: utahta/gopl
func main() {
	var useType string
	flag.StringVar(&useType, "sha", "256", "256, 384, 512. ")
	flag.Parse()

	arg := []byte(flag.Arg(0))
	switch useType {
	case "384":
		fmt.Printf("%x\n", sha512.Sum384(arg))
	case "512":
		fmt.Printf("%x\n", sha512.Sum512(arg))
	default:
		fmt.Printf("%x\n", sha256.Sum256(arg))
	}
}
Exemplo n.º 17
0
func main() {
	scanner := bufio.NewScanner(os.Stdin)
	for scanner.Scan() {
		switch hash {
		case "sha384":
			c := sha512.Sum384(scanner.Bytes())
			fmt.Printf("%x\n", c)
		case "sha512":
			c := sha512.Sum512(scanner.Bytes())
			fmt.Printf("%x\n", c)
		default:
			c := sha256.Sum256(scanner.Bytes())
			fmt.Printf("%x\n", c)
		}
	}
}
Exemplo n.º 18
0
func calcSHA(s, opt string) []byte {
	switch opt {
	case "256":
		res := sha256.Sum256([]byte(s))
		return res[:]
	case "384":
		res := sha512.Sum384([]byte(s))
		return res[:]
	case "512":
		res := sha512.Sum512([]byte(s))
		return res[:]
	default:
		fmt.Println("undefined option.")
		return []byte{}
	}
}
Exemplo n.º 19
0
Arquivo: ex2.go Projeto: yyBeta/gopl
func main() {
	flag.Parse()
	var b []byte
	fmt.Printf("input string to see its SHA%d: ", *bit)
	fmt.Scanln(&b)

	switch *bit {
	case 256:
		fmt.Fprintf(os.Stdout, "%x", sha256.Sum256(b))
	case 384:
		fmt.Fprintf(os.Stdout, "%x", sha512.Sum384(b))
	case 512:
		fmt.Fprintf(os.Stdout, "%x", sha512.Sum512(b))
	default:
		fmt.Fprintf(os.Stderr, "unsupported bit")
	}
}
Exemplo n.º 20
0
Arquivo: ex2.go Projeto: suzuken/gopl
func main() {
	flag.Parse()
	b, err := ioutil.ReadAll(os.Stdin)
	if err != nil {
		panic(err)
	}
	switch *bit {
	case 256:
		fmt.Fprintf(os.Stdout, "%x", sha256.Sum256(b))
	case 384:
		fmt.Fprintf(os.Stdout, "%x", sha512.Sum384(b))
	case 512:
		fmt.Fprintf(os.Stdout, "%x", sha512.Sum512(b))
	default:
		fmt.Fprint(os.Stderr, "unsupported bit")
	}
}
Exemplo n.º 21
0
func main() {
	var hash = flag.String("hash", "sha256", "sha256 | sha384 | sha512")
	flag.Parse()

	s, _ := ioutil.ReadAll(os.Stdin)

	switch *hash {
	case "sha256":
		fmt.Printf("%x\n", sha256.Sum256(s))
	case "sha384":
		fmt.Printf("%x\n", sha512.Sum384(s))
	case "sha512":
		fmt.Printf("%x\n", sha512.Sum512(s))
	default:
		fmt.Printf("%x\n", sha256.Sum256(s))
	}
}
Exemplo n.º 22
0
/*
对称加密,  安全性没有这么简单
 key为任意长度,使用简单
 data为任意长度,使用简单
 使用aes,cbc,32位密码
 输入密码hash使用sha384
 数据padding使用PKCS5Padding
 不会修改输入的数据
 @deprecated
*/
func Encrypt(key []byte, data []byte) (output []byte, err error) {
	keyHash := sha512.Sum384(key)
	aseKey := keyHash[:32]
	cbcIv := keyHash[32:]
	block, err := aes.NewCipher(aseKey)
	if err != nil {
		return nil, err
	}
	blockSize := block.BlockSize()
	paddingSize := blockSize - len(data)%blockSize
	afterCbcSize := paddingSize + len(data)
	output = make([]byte, afterCbcSize)
	copy(output, data)
	copy(output[len(data):], bytes.Repeat([]byte{byte(paddingSize)}, paddingSize))
	blockmode := cipher.NewCBCEncrypter(block, cbcIv)
	blockmode.CryptBlocks(output, output)
	return output, nil
}
Exemplo n.º 23
0
func doHash(p *[]byte, digest string) {
	if digest == "sha512" {
		var hash [sha512.Size]byte
		hash = sha512.Sum512(*p)
		fmt.Printf("%x\n", hash)

	} else if digest == "sha384" {
		var hash [sha512.Size384]byte
		hash = sha512.Sum384(*p)
		fmt.Printf("%x\n", hash)
	} else if digest != "" {
		fmt.Println("Unknown algorithm, we only support sha256, sha384, and sha512")
	} else {
		var hash [sha512.Size256]byte
		hash = sha512.Sum512_256(*p)
		fmt.Printf("%x\n", hash)
	}

}
Exemplo n.º 24
0
func verify(signerKeyFile, userKeyFile, sigFile string) bool {
	rawSigner, err := ioutil.ReadFile(signerKeyFile)
	if err != nil {
		fmt.Printf("Failed to read signature key: %v\n", err)
		return false
	}
	var signer ecdsa.PublicKey
	signer.X, signer.Y = elliptic.Unmarshal(elliptic.P521(), rawSigner)
	if signer.X == nil {
		fmt.Println("Invalid signature key.")
		return false
	}
	signer.Curve = elliptic.P521()

	rawUser, err := ioutil.ReadFile(userKeyFile)
	if err != nil {
		fmt.Printf("Failed to read user key: %v\n", err)
		return false
	}

	x, _ := elliptic.Unmarshal(elliptic.P521(), rawUser)
	if x == nil {
		fmt.Println("Invalid user key.")
		return false
	}

	rawSig, err := ioutil.ReadFile(sigFile)
	if err != nil {
		fmt.Printf("Failed to load signature: %v\n", err)
		return false
	}

	var sig ECDSASignature
	_, err = asn1.Unmarshal(rawSig, &sig)
	if err != nil {
		fmt.Printf("Failed to parse signature: %v\n", err)
		return false
	}

	h := sha512.Sum384(rawUser)
	return ecdsa.Verify(&signer, h[:], sig.R, sig.S)
}
Exemplo n.º 25
0
func main() {
	a := ""
	if len(os.Args) > 1 {
		a = os.Args[1]
	} else {
		a = "sha256"
	}
	text := fmt.Sprint(os.Stdin)
	bytes := []byte(text)
	switch a {
	case "sha384":
		fmt.Fprint(os.Stdout, sha512.Sum512(bytes))
	case "sha512":
		fmt.Fprint(os.Stdout, sha512.Sum384(bytes))
	case "sha256":
		fallthrough
	default:
		fmt.Fprint(os.Stdout, sha256.Sum256(bytes))
	}
}
Exemplo n.º 26
0
// GetCustomImageInfos returns the image infos for window specific
// base images which should always be present.
func (d *Driver) GetCustomImageInfos() ([]CustomImageInfo, error) {
	strData, err := hcsshim.GetSharedBaseImages()
	if err != nil {
		return nil, fmt.Errorf("Failed to restore base images: %s", err)
	}

	type customImageInfoList struct {
		Images []CustomImageInfo
	}

	var infoData customImageInfoList

	if err = json.Unmarshal([]byte(strData), &infoData); err != nil {
		err = fmt.Errorf("JSON unmarshal returned error=%s", err)
		logrus.Error(err)
		return nil, err
	}

	var images []CustomImageInfo

	for _, imageData := range infoData.Images {
		folderName := filepath.Base(imageData.Path)

		// Use crypto hash of the foldername to generate a docker style id.
		h := sha512.Sum384([]byte(folderName))
		id := fmt.Sprintf("%x", h[:32])

		if err := d.Create(id, "", ""); err != nil {
			return nil, err
		}
		// Create the alternate ID file.
		if err := d.setID(id, folderName); err != nil {
			return nil, err
		}

		imageData.ID = id
		images = append(images, imageData)
	}

	return images, nil
}
Exemplo n.º 27
0
func main() {
	reader := bufio.NewReader(os.Stdin)
	fmt.Print("Enter text: ")
	text, err := reader.ReadString('\n')
	if err != nil {
		fmt.Println("Error: reading stdin")
		os.Exit(1)
	}

	switch *ds {
	case 256:
		c := sha256.Sum256([]byte(text))
		fmt.Printf("%x\n", c)
	case 384:
		c := sha512.Sum384([]byte(text))
		fmt.Printf("%x\n", c)
	case 512:
		c := sha512.Sum512([]byte(text))
		fmt.Printf("%x\n", c)
	}
}
Exemplo n.º 28
0
// Cheat function to handle the hash type switch.
func hashMessage(m []byte, h crypto.Hash) []byte {
	switch h {
	case crypto.SHA1:
		sum := sha1.Sum(m)
		return sum[:]
	case crypto.SHA224:
		sum := sha256.Sum224(m)
		return sum[:]
	case crypto.SHA256:
		sum := sha256.Sum256(m)
		return sum[:]
	case crypto.SHA384:
		sum := sha512.Sum384(m)
		return sum[:]
	case crypto.SHA512:
		sum := sha512.Sum512(m)
		return sum[:]
	default:
		return nil
	}
}
Exemplo n.º 29
0
func encrypt(r io.ReadCloser) (*envelope, io.ReadCloser, error) {
	var contents bytes.Buffer
	_, err := io.Copy(&contents, r)
	if err != nil {
		return nil, nil, fmt.Errorf("failed to read content: %v", err)
	}
	err = r.Close()
	if err != nil {
		return nil, nil, fmt.Errorf("failed to close input: %v", err)
	}

	digest := sha512.Sum384(contents.Bytes())
	env, err := generateEnvelope()
	if err != nil {
		return nil, nil, fmt.Errorf("failed to create envelope: %v", err)
	}
	env.sha384 = digest
	out := secretbox.Seal(nil, contents.Bytes(), env.nonce, env.key)
	// TODO: zeroize `contents`
	return env, ioutil.NopCloser(bytes.NewBuffer(out)), nil
}
Exemplo n.º 30
0
Arquivo: main.go Projeto: zDpxq6/go
func main() {
	var s string
	if input.Scan() {
		s = input.Text()
	}
	strings := strings.Split(s, " ")
	if 2 < len(strings) {
		return
	}
	if len(strings) == 1 {
		result := sha256.Sum256([]byte(strings[0]))
		fmt.Printf("%x", result)
	} else if strings[1] == "-sha512" {
		result := sha512.Sum512([]byte(strings[0]))
		fmt.Printf("%x", result)
	} else if strings[1] == "-sha384" {
		result := sha512.Sum384([]byte(strings[0]))
		fmt.Printf("%x", result)
	}
	return
}