X-Git-Url: http://www.git.cypherpunks.ru/?a=blobdiff_plain;f=src%2Fcypherpunks.ru%2Fgogost%2Finternal%2Fgost34112012%2Fhash.go;h=5f6b707d87a05a001326d464693406e10aa60672;hb=91562b3cf4aad503c493aa7b69abfbb07b46e63a;hp=5a8227cd309adc10112888bdd28a539974257bed;hpb=5733007c9d8416d7aec16324b788156168d2e7a6;p=gogost.git diff --git a/src/cypherpunks.ru/gogost/internal/gost34112012/hash.go b/src/cypherpunks.ru/gogost/internal/gost34112012/hash.go index 5a8227c..5f6b707 100644 --- a/src/cypherpunks.ru/gogost/internal/gost34112012/hash.go +++ b/src/cypherpunks.ru/gogost/internal/gost34112012/hash.go @@ -1,10 +1,9 @@ // GoGOST -- Pure Go GOST cryptographic functions library -// Copyright (C) 2015-2016 Sergey Matveev +// Copyright (C) 2015-2019 Sergey Matveev // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. +// the Free Software Foundation, version 3 of the License. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -19,11 +18,15 @@ package gost34112012 import ( + "bytes" "encoding/binary" + "errors" ) const ( BlockSize = 64 + + MarshaledName = "STREEBOG" ) var ( @@ -403,3 +406,43 @@ func l(data *[BlockSize]byte) *[BlockSize]byte { } return r } + +func (h *Hash) MarshalBinary() (data []byte, err error) { + data = make([]byte, len(MarshaledName)+1+8+3*BlockSize+len(h.buf)) + copy(data, []byte(MarshaledName)) + idx := len(MarshaledName) + data[idx] = byte(h.size) + idx += 1 + binary.BigEndian.PutUint64(data[idx:idx+8], h.n) + idx += 8 + copy(data[idx:], h.hsh[:]) + idx += BlockSize + copy(data[idx:], h.chk[:]) + idx += BlockSize + copy(data[idx:], h.tmp[:]) + idx += BlockSize + copy(data[idx:], h.buf) + return +} + +func (h *Hash) UnmarshalBinary(data []byte) error { + if len(data) < len(MarshaledName)+1+8+3*BlockSize { + return errors.New("too short data") + } + if !bytes.HasPrefix(data, []byte(MarshaledName)) { + return errors.New("no hash name prefix") + } + idx := len(MarshaledName) + h.size = int(data[idx]) + idx += 1 + h.n = binary.BigEndian.Uint64(data[idx : idx+8]) + idx += 8 + copy(h.hsh[:], data[idx:]) + idx += BlockSize + copy(h.chk[:], data[idx:]) + idx += BlockSize + copy(h.tmp[:], data[idx:]) + idx += BlockSize + h.buf = data[idx:] + return nil +}