github.com/cheng762/platon-go@v1.8.17-0.20190529111256-7deff2d7be26/swarm/api/encrypt.go (about)

     1  // Copyright 2016 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package api
    18  
    19  import (
    20  	"encoding/binary"
    21  	"errors"
    22  
    23  	"github.com/PlatONnetwork/PlatON-Go/crypto/sha3"
    24  	"github.com/PlatONnetwork/PlatON-Go/swarm/storage/encryption"
    25  )
    26  
    27  type RefEncryption struct {
    28  	refSize int
    29  	span    []byte
    30  }
    31  
    32  func NewRefEncryption(refSize int) *RefEncryption {
    33  	span := make([]byte, 8)
    34  	binary.LittleEndian.PutUint64(span, uint64(refSize))
    35  	return &RefEncryption{
    36  		refSize: refSize,
    37  		span:    span,
    38  	}
    39  }
    40  
    41  func (re *RefEncryption) Encrypt(ref []byte, key []byte) ([]byte, error) {
    42  	spanEncryption := encryption.New(key, 0, uint32(re.refSize/32), sha3.NewKeccak256)
    43  	encryptedSpan, err := spanEncryption.Encrypt(re.span)
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  	dataEncryption := encryption.New(key, re.refSize, 0, sha3.NewKeccak256)
    48  	encryptedData, err := dataEncryption.Encrypt(ref)
    49  	if err != nil {
    50  		return nil, err
    51  	}
    52  	encryptedRef := make([]byte, len(ref)+8)
    53  	copy(encryptedRef[:8], encryptedSpan)
    54  	copy(encryptedRef[8:], encryptedData)
    55  
    56  	return encryptedRef, nil
    57  }
    58  
    59  func (re *RefEncryption) Decrypt(ref []byte, key []byte) ([]byte, error) {
    60  	spanEncryption := encryption.New(key, 0, uint32(re.refSize/32), sha3.NewKeccak256)
    61  	decryptedSpan, err := spanEncryption.Decrypt(ref[:8])
    62  	if err != nil {
    63  		return nil, err
    64  	}
    65  
    66  	size := binary.LittleEndian.Uint64(decryptedSpan)
    67  	if size != uint64(len(ref)-8) {
    68  		return nil, errors.New("invalid span in encrypted reference")
    69  	}
    70  
    71  	dataEncryption := encryption.New(key, re.refSize, 0, sha3.NewKeccak256)
    72  	decryptedRef, err := dataEncryption.Decrypt(ref[8:])
    73  	if err != nil {
    74  		return nil, err
    75  	}
    76  
    77  	return decryptedRef, nil
    78  }