github.com/prysmaticlabs/prysm@v1.4.4/shared/keystore/utils.go (about)

     1  // Copyright 2014 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // Modified by Prysmatic Labs 2018
     5  //
     6  // The go-ethereum library is free software: you can redistribute it and/or modify
     7  // it under the terms of the GNU Lesser General Public License as published by
     8  // the Free Software Foundation, either version 3 of the License, or
     9  // (at your option) any later version.
    10  //
    11  // The go-ethereum library is distributed in the hope that it will be useful,
    12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    14  // GNU Lesser General Public License for more details.
    15  //
    16  // You should have received a copy of the GNU Lesser General Public License
    17  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    18  
    19  package keystore
    20  
    21  import (
    22  	"crypto/aes"
    23  	"crypto/cipher"
    24  	"encoding/hex"
    25  	"fmt"
    26  	"time"
    27  
    28  	"github.com/prysmaticlabs/prysm/shared/bls"
    29  	"github.com/prysmaticlabs/prysm/shared/timeutils"
    30  )
    31  
    32  func aesCTRXOR(key, inText, iv []byte) ([]byte, error) {
    33  	// AES-128 is selected due to size of encryptKey.
    34  	aesBlock, err := aes.NewCipher(key)
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  	stream := cipher.NewCTR(aesBlock, iv)
    39  	outText := make([]byte, len(inText))
    40  	stream.XORKeyStream(outText, inText)
    41  	return outText, err
    42  }
    43  
    44  func ensureInt(x interface{}) int {
    45  	res, ok := x.(int)
    46  	if !ok {
    47  		res = int(x.(float64))
    48  	}
    49  	return res
    50  }
    51  
    52  // keyFileName implements the naming convention for keyfiles:
    53  // UTC--<created_at UTC ISO8601>-<first 8 character of address hex>
    54  func keyFileName(pubkey bls.PublicKey) string {
    55  	ts := timeutils.Now().UTC()
    56  	return fmt.Sprintf("UTC--%s--%s", toISO8601(ts), hex.EncodeToString(pubkey.Marshal())[:8])
    57  }
    58  
    59  func toISO8601(t time.Time) string {
    60  	var tz string
    61  	name, offset := t.Zone()
    62  	if name == "UTC" {
    63  		tz = "Z"
    64  	} else {
    65  		tz = fmt.Sprintf("%03d00", offset/3600)
    66  	}
    67  	return fmt.Sprintf("%04d-%02d-%02dT%02d-%02d-%02d.%09d%s", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), tz)
    68  }