]> Cypherpunks.ru repositories - gogost.git/blobdiff - src/cypherpunks.ru/gogost/internal/gost34112012/hash.go
Forbid any later GNU GPL versions autousage
[gogost.git] / src / cypherpunks.ru / gogost / internal / gost34112012 / hash.go
index 5a8227cd309adc10112888bdd28a539974257bed..5f6b707d87a05a001326d464693406e10aa60672 100644 (file)
@@ -1,10 +1,9 @@
 // GoGOST -- Pure Go GOST cryptographic functions library
-// Copyright (C) 2015-2016 Sergey Matveev <stargrave@stargrave.org>
+// Copyright (C) 2015-2019 Sergey Matveev <stargrave@stargrave.org>
 //
 // 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
 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
+}