本文整理汇总了Golang中encoding/hex.DecodedLen函数的典型用法代码示例。如果您正苦于以下问题:Golang DecodedLen函数的具体用法?Golang DecodedLen怎么用?Golang DecodedLen使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了DecodedLen函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: SignSHA256
// Sign the given hex-endcoded hash checksum and return a Signature
// @@TODO Remove this -- undeeded
func (pk PrivateKey) SignSHA256(hexbytes []byte) (Signature, error) {
if hex.DecodedLen(len(hexbytes)) != sha256.Size {
return nil, ErrPrivateKeySHA256
}
// Decode hex bytes into raw bytes
decodedBytes := make([]byte, hex.DecodedLen(len(hexbytes)))
_, err := hex.Decode(decodedBytes, hexbytes)
if err != nil {
return nil, errors.Wrap(err, ErrPrivateKeySHA256)
}
// Get the rsa cryptokey for signing
cryptoKey, err := pk.GetCryptoKey()
if err != nil {
return nil, errors.Wrap(err, ErrPrivatKeySign)
}
// Compute the signature and return the results
rawSignature, err := rsa.SignPKCS1v15(rand.Reader, cryptoKey, crypto.SHA256, decodedBytes)
if err != nil {
return nil, errors.Wrap(err, ErrPrivatKeySign)
}
return Signature(rawSignature), nil
}
示例2: ConvertHexString
func ConvertHexString(line []byte) (result []byte, err error) {
buf := &bytes.Buffer{}
for i, count := 0, len(line); i < count; i++ {
b := line[i]
if b == '\\' {
if i+1 < count && line[i+1] == 'x' {
if i+2+2 >= count {
err = errors.New("invalid character \\x")
return
}
b34hex := line[i+2 : i+2+2]
b34 := make([]byte, hex.DecodedLen(len(b34hex)))
_, err = hex.Decode(b34, b34hex)
if err != nil {
return
}
buf.Write(b34)
i += 3 // skip \xff
} else {
buf.WriteByte('\\')
}
} else {
buf.WriteByte(b)
}
}
result = buf.Bytes()
return
}
示例3: main
func main() {
flag.StringVar(&Settings.src, "src", "src", "name for traffic src")
flag.StringVar(&Settings.dst, "dst", "dst", "name for traffic dst")
flag.StringVar(&Settings.graphite, "graphite", "", "name for traffic dst")
flag.StringVar(&Settings.prefix, "prefix", "", "prefix for reported timings")
flag.Parse()
if Settings.graphite == "" || Settings.prefix == "" {
log.Fatal("must supply graphite server and prefix")
}
report.NewRecorder().
ReportTo(Settings.graphite, Settings.prefix).
LogToConsole().
SetAsDefault()
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
encoded := scanner.Bytes()
buf := make([]byte, hex.DecodedLen(len(encoded)))
hex.Decode(buf, encoded)
kind := buf[0]
if kind == RequestFlag {
os.Stdout.Write(encoded)
}
go handle(kind, buf)
}
}
示例4: getShaFor
func getShaFor(url string) ([]byte, error) {
res, err := http.Get(url + ".sha1")
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("HTTP Status: %d", res.StatusCode)
}
b, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
b = bytes.TrimSpace(b)
b = b[:40]
s := make([]byte, hex.DecodedLen(len(b)))
if _, err := hex.Decode(s, b); err != nil {
return nil, err
}
return s, nil
}
示例5: Decode
// Decode decodes the byte-reversed hexadecimal string encoding of a Hash to a
// destination.
func Decode(dst *Hash, src string) error {
// Return error if hash string is too long.
if len(src) > MaxHashStringSize {
return ErrHashStrSize
}
// Hex decoder expects the hash to be a multiple of two. When not, pad
// with a leading zero.
var srcBytes []byte
if len(src)%2 == 0 {
srcBytes = []byte(src)
} else {
srcBytes = make([]byte, 1+len(src))
srcBytes[0] = '0'
copy(srcBytes[1:], src)
}
// Hex decode the source bytes to a temporary destination.
var reversedHash Hash
_, err := hex.Decode(reversedHash[HashSize-hex.DecodedLen(len(srcBytes)):], srcBytes)
if err != nil {
return err
}
// Reverse copy from the temporary hash to destination. Because the
// temporary was zeroed, the written result will be correctly padded.
for i, b := range reversedHash[:HashSize/2] {
dst[i], dst[HashSize-1-i] = reversedHash[HashSize-1-i], b
}
return nil
}
示例6: Scan
// Scan implements database/sql Scanner interface.
// It expectes EWKB with SRID 4326 (WGS 84).
func (p *PostGISPoint) Scan(value interface{}) error {
if value == nil {
*p = PostGISPoint{}
return nil
}
v, ok := value.([]byte)
if !ok {
return fmt.Errorf("pq_types: expected []byte, got %T (%v)", value, value)
}
ewkb := make([]byte, hex.DecodedLen(len(v)))
n, err := hex.Decode(ewkb, v)
if err != nil {
return err
}
var ewkbP ewkbPoint
err = binary.Read(bytes.NewReader(ewkb[:n]), binary.LittleEndian, &ewkbP)
if err != nil {
return err
}
if ewkbP.ByteOrder != 1 || ewkbP.WkbType != 0x20000001 || ewkbP.SRID != 4326 {
return fmt.Errorf("pq_types: unexpected ewkb %#v", ewkbP)
}
*p = ewkbP.Point
return nil
}
示例7: Decode
// Decode extracts PDU from ASCII frame and verify LRC.
func (mb *asciiPackager) Decode(adu []byte) (pdu *ProtocolDataUnit, err error) {
pdu = &ProtocolDataUnit{}
// Slave address
address, err := readHex(adu[1:])
if err != nil {
return
}
// Function code
if pdu.FunctionCode, err = readHex(adu[3:]); err != nil {
return
}
// Data
dataEnd := len(adu) - 4
data := adu[5:dataEnd]
pdu.Data = make([]byte, hex.DecodedLen(len(data)))
if _, err = hex.Decode(pdu.Data, data); err != nil {
return
}
// LRC
lrcVal, err := readHex(adu[dataEnd:])
if err != nil {
return
}
// Calculate checksum
var lrc lrc
lrc.reset()
lrc.pushByte(address).pushByte(pdu.FunctionCode).pushBytes(pdu.Data)
if lrcVal != lrc.value() {
err = fmt.Errorf("modbus: response lrc '%v' does not match expected '%v'", lrcVal, lrc.value())
return
}
return
}
示例8: copyBinary
func copyBinary(sourceName, targetName, sourceDir, targetDir string) error {
source := filepath.Join(sourceDir, sourceName)
dest := filepath.Join(targetDir, targetName)
if _, err := os.Stat(source); err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("missing file %s", source)
}
return err
}
if err := CopyFile(source, dest, 0755); err != nil {
return err
}
if b, err := ioutil.ReadFile(source + ".sha256"); err == nil {
if i := bytes.IndexRune(b, ' '); i > 0 {
b = b[:i]
}
expectedHash := make([]byte, hex.DecodedLen(len(b)))
if _, err := hex.Decode(expectedHash, b); err != nil {
return err
}
if err := hashCheck(dest, expectedHash, sha256.New()); err != nil {
return err
}
}
return nil
}
示例9: WireMsgToComposedMsg
// WireMsgToComposedMsg translates a multipart ZMQ messages received from a socket into
// a ComposedMsg struct and a slice of return identities. This includes verifying the
// message signature.
func WireMsgToComposedMsg(msgparts [][]byte, signkey []byte) (msg ComposedMsg,
identities [][]byte, err error) {
i := 0
for string(msgparts[i]) != "<IDS|MSG>" {
i++
}
identities = msgparts[:i]
// msgparts[i] is the delimiter
// Validate signature
if len(signkey) != 0 {
mac := hmac.New(sha256.New, signkey)
for _, msgpart := range msgparts[i+2 : i+6] {
mac.Write(msgpart)
}
signature := make([]byte, hex.DecodedLen(len(msgparts[i+1])))
hex.Decode(signature, msgparts[i+1])
if !hmac.Equal(mac.Sum(nil), signature) {
return msg, nil, &InvalidSignatureError{}
}
}
json.Unmarshal(msgparts[i+2], &msg.Header)
json.Unmarshal(msgparts[i+3], &msg.Parent_header)
json.Unmarshal(msgparts[i+4], &msg.Metadata)
json.Unmarshal(msgparts[i+5], &msg.Content)
return
}
示例10: randomIdentifierOfLength
// RandomIdentifierOfLength returns a string
// composed of random integers (from `crypto/rand`)
// with exactly len characters.
func randomIdentifierOfLength(len int) string {
buflen := en.DecodedLen(len)
buffer := make([]byte, buflen)
rand.Read(buffer)
return en.EncodeToString(buffer)
}
示例11: createRandomKey
func createRandomKey(length int) (string, error) {
var buffer []byte = make([]byte, hex.DecodedLen(length))
if _, err := rand.Read(buffer); err != nil {
return "", err
} else {
return hex.EncodeToString(buffer), nil
}
}
示例12: createRandomToken
func createRandomToken() (string, error) {
var buffer []byte = make([]byte, hex.DecodedLen(TOKEN_LENGTH))
if _, err := rand.Read(buffer); err != nil {
return "", err
} else {
return hex.EncodeToString(buffer), nil
}
}
示例13: Unhex
// Unhex bytes
func (c *SCrypto) Unhex(src []byte, maxLen int) []byte {
dst := make([]byte, hex.DecodedLen(len(src)))
hex.Decode(dst, src)
if len(dst) > maxLen {
// avoid extraneous padding
dst = dst[:maxLen]
}
return dst
}
示例14: parseShortString
func parseShortString(s string) (u UUID, e error) {
b := []byte(s)
if hex.DecodedLen(len(s)) != UUIDLen {
e = fmt.Errorf("uuid: wrong string length for decode")
return
}
_, e = hex.Decode(u[:], b)
return
}
示例15: Set
func (p *PublicKey) Set(value string) error {
if hex.DecodedLen(len(value)) != ed25519.PublicKeySize {
return fmt.Errorf("not a valid public key: wrong size")
}
if _, err := hex.Decode(p[:], []byte(value)); err != nil {
return fmt.Errorf("not a valid public key: %v", err)
}
return nil
}