func keyDescToJson()

in cli/key_cmds.go [16:50]


func keyDescToJson(path string, body []byte, desc ikey.Desc) (string, error) {
	type Key struct {
		Path       string `json:"path"`
		Type       string `json:"type"`
		Algorithm  string `json:"algorithm"`
		Hash       string `json:"hash"`
		FileSha256 string `json:"file_sha256"`
	}

	var typ string
	if desc.Private {
		typ = "private"
	} else {
		typ = "public"
	}

	h := sha256.Sum256(body)
	fileHash := h[:]

	k := Key{
		Path:       path,
		Type:       typ,
		Algorithm:  desc.Algorithm,
		Hash:       hex.EncodeToString(desc.Hash),
		FileSha256: hex.EncodeToString(fileHash),
	}

	j, err := json.MarshalIndent(k, "", "    ")
	if err != nil {
		return "", errors.Wrapf(err,
			"internal error: failed to marshal key description")
	}

	return string(j), nil
}