// ToMnemonic maps the final enciphered cipher seed to a human readable 24-word
// mnemonic phrase. The password is optional, as if it isn't specified aezeed
// will be used in its place.
487545
func (c *CipherSeed) ToMnemonic(pass []byte) (Mnemonic, error) {
	// First, we'll convert the valid seed triple into an aez cipher text
	// with our KDF salt appended to it.
	cipherText, err := c.encipher(pass)
	if err != nil {
		return Mnemonic{}, nil
	}

	// Now that we have our cipher text, we'll convert it into a mnemonic
	// phrase.
	return cipherTextToMnemonic(cipherText)
}
43664
func (m *MT19937) SeedArray(initKey [NN]uint64) {
	var i, j uint64

	m.Seed(19650218)
	i = 1
	j = 0

	k := NN
	if NN <= len(initKey) {
		k = len(initKey)
	}

	for ; k != 0; k-- {
		m.mt[i] = (m.mt[i] ^ ((m.mt[i-1] ^ (m.mt[i-1] >> 62)) * 3935559000370003845)) + initKey[j] + j /* non linear */

		i++
		j++
		if i >= NN {
			m.mt[0] = m.mt[NN-1]
			i = 1
		}
		if j >= uint64(len(initKey)) {
			j = 0
		}
	}

	for k = NN - 1; k != 0; k-- {
		m.mt[i] = (m.mt[i] ^ ((m.mt[i-1] ^ (m.mt[i-1] >> 62)) * 2862933555777941757)) - i /* non linear */
		i++
		if i >= NN {
			m.mt[0] = m.mt[NN-1]
			i = 1
		}
	}

	m.mt[0] = 1 << 63 /* MSB is 1; assuring non-zero initial array */
}
482298
func (m memNode) PubKey() [33]byte {
	var n [33]byte
	copy(n[:], m.pub.SerializeCompressed())

	return n
}
1145013
func (whisper *Whisper) GenerateSymKey() (string, error) {
	key, err := generateSecureRandomData(aesKeyLength)
	if err != nil {
		return "", err
	} else if !validateDataIntegrity(key, aesKeyLength) {
		return "", fmt.Errorf("error in GenerateSymKey: crypto/rand failed to generate random data")
	}

	id, err := GenerateRandomID()
	if err != nil {
		return "", fmt.Errorf("failed to generate ID: %s", err)
	}

	whisper.keyMu.Lock()
	defer whisper.keyMu.Unlock()

	if whisper.symKeys[id] != nil {
		return "", fmt.Errorf("failed to generate unique ID")
	}
	whisper.symKeys[id] = key
	return id, nil
}
569489
func (c *ServerContext) DecryptMessage(msg []byte, seqno uint32) (uint32, []byte, error) {
	return decryptMessage(c.sctxt, msg, seqno)
}
1249558
func (r *SplicedMemory) ReadMemory(buf []byte, addr uintptr) (n int, err error) {
	started := false
	for _, entry := range r.readers {
		if entry.offset+entry.length < addr {
			if !started {
				continue
			}
			return n, fmt.Errorf("hit unmapped area at %v after %v bytes", addr, n)
		}

		// Don't go past the region.
		pb := buf
		if addr+uintptr(len(buf)) > entry.offset+entry.length {
			pb = pb[:entry.offset+entry.length-addr]
		}
		pn, err := entry.reader.ReadMemory(pb, addr)
		n += pn
		if err != nil || pn != len(pb) {
			return n, err
		}
		buf = buf[pn:]
		addr += uintptr(pn)
		if len(buf) == 0 {
			// Done, don't bother scanning the rest.
			return n, nil
		}
	}
	if n == 0 {
		return 0, fmt.Errorf("offset %v did not match any regions", addr)
	}
	return n, nil
}
174073
func (s *SignEncrypter) EncryptResume(r io.Reader, nonce signencrypt.Nonce, encKey signencrypt.SecretboxKey, signKey signencrypt.SignKey, verifyKey signencrypt.VerifyKey) (io.Reader, error) {
	s.encKey = encKey
	s.signKey = signKey
	s.verifyKey = verifyKey
	return signencrypt.NewEncodingReader(s.encKey, s.signKey, kbcrypto.SignaturePrefixChatAttachment, nonce, r), nil
}
669995
func (msg *sentMessage) encryptSymmetric(key []byte) (err error) {
	if !validateDataIntegrity(key, aesKeyLength) {
		return errors.New("invalid key provided for symmetric encryption, size: " + strconv.Itoa(len(key)))
	}
	block, err := aes.NewCipher(key)
	if err != nil {
		return err
	}
	aesgcm, err := cipher.NewGCM(block)
	if err != nil {
		return err
	}
	salt, err := generateSecureRandomData(aesNonceLength) // never use more than 2^32 random nonces with a given key
	if err != nil {
		return err
	}
	encrypted := aesgcm.Seal(nil, salt, msg.Raw, nil)
	msg.Raw = append(encrypted, salt...)
	return nil
}
783817
func NewSeed() ([]byte, error) {
	// Well that easy, just make go read 256 random bytes into a slice
	s := make([]byte, 256)
	_, err := rand.Read(s)
	return s, err
}
66174
func NewMasterKey(seed []byte) (*Key, error) {
	// Generate key and chaincode
	hmac := hmac.New(sha512.New, []byte("Bitcoin seed"))
	_, err := hmac.Write(seed)
	if err != nil {
		return nil, err
	}
	intermediary := hmac.Sum(nil)

	// Split it into our key and chain code
	keyBytes := intermediary[:32]
	chainCode := intermediary[32:]

	// Validate key
	err = validatePrivateKey(keyBytes)
	if err != nil {
		return nil, err
	}

	// Create the key struct
	key := &Key{
		Version:     PrivateWalletVersion,
		ChainCode:   chainCode,
		Key:         keyBytes,
		Depth:       0x0,
		ChildNumber: []byte{0x00, 0x00, 0x00, 0x00},
		FingerPrint: []byte{0x00, 0x00, 0x00, 0x00},
		IsPrivate:   true,
	}

	return key, nil
}
1214245
func ToMap(ctx context.Context) map[string]interface{} {
	if ctx == nil {
		return map[string]interface{}{}
	}
	p := ctx.Value(constants.PropagateCtxKey)
	if p != nil {
		return p.(map[string]interface{})
	}
	return map[string]interface{}{}
}
166010
func (s *Secret) Bytes() ([]byte, error) {
	if s.d == nil {
		return []byte{}, nil
	}

	buf := &bytes.Buffer{}
	if pw, found := s.d[passwordKey]; found {
		if sv, ok := pw.(string); ok {
			_, _ = buf.WriteString(sv)
		}
	}
	_, _ = buf.WriteString("\n")
	keys := make([]string, 0, len(s.d))
	for k := range s.d {
		keys = append(keys, k)
	}
	sort.Strings(keys)
	for _, k := range keys {
		v := s.d[k]
		if k == passwordKey {
			continue
		}
		_, _ = buf.WriteString(k)
		_, _ = buf.WriteString(": ")
		if sv, ok := v.(string); ok {
			_, _ = buf.WriteString(sv)
		}
		_, _ = buf.WriteString("\n")
	}
	return buf.Bytes(), nil
}
535499
func encryptSeed(seed []byte, auth []byte, scryptN, scryptP int) ([]byte, error) {
	cryptoStruct, err := keystore.EncryptDataV3(seed, auth, scryptN, scryptP)
	if err != nil {
		return nil, err
	}
	return json.Marshal(&encryptedSeedStorage{"Clef seed", 1, cryptoStruct})
}
121717
func (p *Decoder) parseMetaMsg(e *Event) (nextChunkType, bool, error) {
	var err error
	// channels aren't really channels
	switch e.MsgChan {
	case 0xF:
		var b byte
		if b, err = p.ReadByte(); err != nil {
			return eventChunk, false, err
		}
		e.Cmd = uint8(b)

		switch e.Cmd {

		// Sequence Number
		//This optional event, which must occur at the beginning of a
		//track, before any nonzero delta-times, and before any
		//transmittable MIDI events, specifies the number of a sequence. In a
		//format 2 MIDI File, it is used to identify each "pattern" so that a
		//"song" sequence using the Cue message to refer to the patterns. If
		//the ID numbers are omitted, the sequences' locations in order in the
		//file are used as defaults. In a format 0 or 1 MIDI File, which only
		//contain one sequence, this number should be contained in the first
		//(or only) track. If transfer of several multitrack sequences is
		//required, this must be done as a group of format 1 files, each with
		//a different sequence number.
		case 0x0:
			if b, err = p.ReadByte(); err != nil {
				return eventChunk, false, err
			}
			if uint8(b) != 0 {
				var seqB []byte
				if err = p.Read(seqB); err != nil {
					return eventChunk, false, err
				}
				e.SeqNum = binary.BigEndian.Uint16(seqB)
			}

		// Text Event
		//Any amount of text describing anything. It is a good idea to put
		//a text event right at the beginning of a track, with the name of the
		//track, a description of its intended orchestration, and any other
		//information which the user wants to put there. Text events may also
		//occur at other times in a track, to be used as lyrics, or descriptions
		//of cue points. The text in this event should be printable ASCII
		//characters for maximum interchange. However, other characters codes
		//using the high-order bit may be used for interchange of files between
		//different programs on the same computer which supports an extended
		//character set. Programs on a computer which does not support
		//non-ASCII characters should ignore those characters.
		case 0x01:
			if e.Text, _, err = p.VarLenTxt(); err != nil {
				return eventChunk, false, err
			}

		// Copyright Notice
		case 0x02:
			if e.Copyright, _, err = p.VarLenTxt(); err != nil {
				return eventChunk, false, err
			}

		// Sequence/Track Name
		//If in a format 0 track, or the first track in a format 1 file, the
		//name of the sequence. Otherwise, the name of the track.
		case 0x03:
			if e.SeqTrackName, _, err = p.VarLenTxt(); err != nil {
				return eventChunk, false, err
			}

		// Instrument name
		//A description of the type of instrumentation to be used in that track.
		//May be used with the MIDI Prefix meta-event to specify which MIDI
		//channel the description applies to, or the channel may be specified
		//as text in the event itself.
		//
		case 0x04:
			if e.InstrumentName, _, err = p.VarLenTxt(); err != nil {
				return eventChunk, false, err
			}

		// Lyric
		case 0x05:
			if e.Lyric, _, err = p.VarLenTxt(); err != nil {
				return eventChunk, false, err
			}

		// Marker
		//Normally in a format 0 track, or the first track in a format 1
		//file. The name of that point in the sequence, such as a rehersal letter
		case 0x06:
			if e.Marker, _, err = p.VarLenTxt(); err != nil {
				return eventChunk, false, err
			}

		// Cue point
		case 0x07:
			if e.CuePoint, _, err = p.VarLenTxt(); err != nil {
				return eventChunk, false, err
			}

		// MIDI Channel Prefix
		//The MIDI channel (0-15) containted in this event may be used
		//to associate a MIDI channel with all events which follow, including
		//System exclusive and meta-events. This channel is "effective" until
		//the next normal MIDI event (which contains a channel) or the next MIDI
		//Channel Prefix meta-event. If MIDI channels refer to "tracks", this
		//message may into a format 0 file, keeping their non-MIDI data
		//associated with a track. This capability is also present in Yamaha's
		//ESEQ file format.
		case 0x20:
			var b byte
			if b, err = p.ReadByte(); err != nil {
				return eventChunk, false, err
			}
			if uint8(b) != 1 {
				return eventChunk, false, fmt.Errorf("MIDI Channel Prefix event error - %s", ErrUnexpectedData)
			}

			if e.Channel, err = p.ReadByte(); err != nil {
				return eventChunk, false, err
			}

		// End of track
		//This event is not optional. It is included so that an exact
		//ending point may be specified for the track, so that an exect length,
		//which is necessary for tracks which are looped or concatenated.
		case 0x2F:
			_, err = p.ReadByte()
			return trackChunk, true, err

			/*
			   Set Tempo(in microseconds per MIDI quarter-note)
			      This meta event sets the sequence tempo in terms of microseconds per quarter-note which is encoded in three bytes.
			      It usually is found in the first track chunk, time-aligned to occur at the same time as a MIDI clock message
			      to promote more accurate synchronization. If no set tempo event is present, 120 beats per minute is assumed.
			      The following formula's can be used to translate the tempo from microseconds per quarter-note to beats per minute and back.
			*/
		case 0x51:
			var l uint32
			if l, _, err = p.VarLen(); err != nil {
				return eventChunk, false, err
			}
			if l != 3 {
				return eventChunk, false, fmt.Errorf("Set Tempo event error - %s", ErrUnexpectedData)
			}

			if e.MsPerQuartNote, err = p.Uint24(); err != nil {
				return eventChunk, false, err
			}

			e.Bpm = 60000000 / e.MsPerQuartNote

			/*
			     SMPTE Offset
			   This meta event is used to specify the SMPTE starting point offset from the beginning of the track.
			   It is defined in terms of hours, minutes, seconds, frames and sub-frames (always 100 sub-frames per frame,
			   no matter what sub-division is specified in the MIDI header chunk).
			   The byte used to specify the hour offset also specifies the frame rate in the following format:
			   0rrhhhhhh where rr is two bits for the frame rate where 00=24 fps, 01=25 fps, 10=30 fps (drop frame),
			   11=30 fps and hhhhhh is six bits for the hour (0-23). The hour byte's top bit is always 0.
			   The frame byte's possible range depends on the encoded frame rate in the hour byte.
			   A 25 fps frame rate means that a maximum value of 24 may be set for the frame byte.
			*/
		case 0x54:
			var l uint32
			if l, _, err = p.VarLen(); err != nil {
				return eventChunk, false, err
			}
			if l != 5 {
				return eventChunk, false, fmt.Errorf("error parsing SMPTE Offset - %s (%d)", ErrUnexpectedData, l)
			}
			so := &SmpteOffset{}
			if err = p.Read(&so.Hour); err != nil {
				return eventChunk, false, err
			}
			if err = p.Read(&so.Min); err != nil {
				return eventChunk, false, err
			}
			if err = p.Read(&so.Sec); err != nil {
				return eventChunk, false, err
			}
			if err = p.Read(&so.Fr); err != nil {
				return eventChunk, false, err
			}
			if err = p.Read(&so.SubFr); err != nil {
				return eventChunk, false, err
			}
			e.SmpteOffset = so

			// Time signature
			// FF 58 04 nn dd cc bb Time Signature
			// The time signature is expressed as four numbers. nn and dd
			// represent the numerator and denominator of the time signature as it
			// would be notated. The denominator is a negative power of two: 2
			// represents a quarter-note, 3 represents an eighth-note, etc.
			// The cc parameter expresses the number of MIDI clocks in a
			// metronome click. The bb parameter expresses the number of
			// notated 32nd-notes in a MIDI quarter-note (24 MIDI clocks). This
			// was added because there are already multiple programs which allow a
			// user to specify that what MIDI thinks of as a quarter-note (24 clocks)
			// is to be notated as, or related to in terms of, something else.
			//
			// This meta event is used to set a sequences time signature.
			// The time signature defined with 4 bytes, a numerator, a denominator, a metronome
			// pulse and number of 32nd notes per MIDI quarter-note. The numerator is specified as
			// a literal value, but the denominator is specified as (get ready) the value to which
			// the power of 2 must be raised to equal the number of subdivisions per whole note.
			// For example, a value of 0 means a whole note because 2 to the power of 0 is 1
			// (whole note), a value of 1 means a half-note because 2 to the power of 1 is 2
			// (half-note), and so on. The metronome pulse specifies how often the metronome should
			// click in terms of the number of clock signals per click, which come at a rate of 24
			// per quarter-note. For example, a value of 24 would mean to click once every quarter-note
			// (beat) and a value of 48 would mean to click once every half-note (2 beats).
			// And finally, the fourth byte specifies the number of 32nd notes per 24 MIDI clock signals.
			// This value is usually 8 because there are usually 8 32nd notes in a quarter-note.
			// At least one Time Signature Event should appear in the first track chunk
			// (or all track chunks in a Type 2 file) before any non-zero delta time events.
			// If one is not specified 4/4, 24, 8 should be assumed.
		case 0x58:
			var l uint32
			if l, _, err = p.VarLen(); err != nil {
				return eventChunk, false, err
			}

			if l != 4 {
				return eventChunk, false, fmt.Errorf("error parsing TimeSignature - %s (%d)", ErrUnexpectedData, l)
			}

			var num, denom, clocksPerClick, thirtySecondNotesPerQuarter byte
			if num, err = p.ReadByte(); err != nil {
				return eventChunk, false, err
			}
			//The denominator is a neqative power of two: 2
			//represents a quarter-note, 3 represents an eighth-note, etc
			if denom, err = p.ReadByte(); err != nil {
				return eventChunk, false, err
			}
			if clocksPerClick, err = p.ReadByte(); err != nil {
				return eventChunk, false, err
			}
			// The number of notated 32nd-notes in a MIDI quarter-note (24 MIDI clocks).
			if thirtySecondNotesPerQuarter, err = p.ReadByte(); err != nil {
				return eventChunk, false, err
			}

			e.TimeSignature = &TimeSignature{uint8(num), uint8(denom), uint8(clocksPerClick), uint8(thirtySecondNotesPerQuarter)}

		// Key signature
		// This meta event is used to specify the key (number of sharps or flats) and scale (major or minor) of a sequence.
		// A positive value for the key specifies the number of sharps and a negative value specifies the number of flats.
		// A value of 0 for the scale specifies a major key and a value of 1 specifies a minor key.
		case 0x59:
			var l uint32
			if l, _, err = p.VarLen(); err != nil {
				return eventChunk, false, err
			}
			if l != 2 {
				return eventChunk, false, fmt.Errorf("Time Signature length not 2 as expected but %d", l)
			}

			// key
			b, err := p.ReadByte()
			if err != nil {
				return eventChunk, false, err
			}
			e.Key = int32(b)

			// scale
			b, err = p.ReadByte()
			if err != nil {
				return eventChunk, false, err
			}
			e.Scale = uint32(b)

		// Sequencer Specific
		//This meta event is used to specify information specific to a hardware or software sequencer.
		// The first Data byte (or three bytes if the first byte is 0) specifies the manufacturer's ID
		// and the following bytes contain information specified by the manufacturer.
		// The individual manufacturers may document this information in their respective manuals.
		case 0x7F:
			var l uint32
			if l, _, err = p.VarLen(); err != nil {
				return eventChunk, false, err
			}
			// Information not currently stored
			tmp := make([]byte, l)
			err = p.Read(tmp)
		default:
			fmt.Printf("Skipped meta cmd %#X\n", e.Cmd)
		}
	}

	return eventChunk, true, nil
}
297016
func (q Query) ToMapByT(result interface{},
	keySelectorFn interface{}, valueSelectorFn interface{}) {
	keySelectorGenericFunc, err := newGenericFunc(
		"ToMapByT", "keySelectorFn", keySelectorFn,
		simpleParamValidator(newElemTypeSlice(new(genericType)), newElemTypeSlice(new(genericType))),
	)
	if err != nil {
		panic(err)
	}

	keySelectorFunc := func(item interface{}) interface{} {
		return keySelectorGenericFunc.Call(item)
	}

	valueSelectorGenericFunc, err := newGenericFunc(
		"ToMapByT", "valueSelectorFn", valueSelectorFn,
		simpleParamValidator(newElemTypeSlice(new(genericType)), newElemTypeSlice(new(genericType))),
	)
	if err != nil {
		panic(err)
	}

	valueSelectorFunc := func(item interface{}) interface{} {
		return valueSelectorGenericFunc.Call(item)
	}

	q.ToMapBy(result, keySelectorFunc, valueSelectorFunc)
}
878439
func NewMnemonic(entropy []byte) (string, error) {
	// Compute some lengths for convenience.
	entropyBitLength := len(entropy) * 8
	checksumBitLength := entropyBitLength / 32
	sentenceLength := (entropyBitLength + checksumBitLength) / 11

	// Validate that the requested size is supported.
	err := validateEntropyBitSize(entropyBitLength)
	if err != nil {
		return "", err
	}

	// Add checksum to entropy.
	entropy = addChecksum(entropy)

	// Break entropy up into sentenceLength chunks of 11 bits.
	// For each word AND mask the rightmost 11 bits and find the word at that index.
	// Then bitshift entropy 11 bits right and repeat.
	// Add to the last empty slot so we can work with LSBs instead of MSB.

	// Entropy as an int so we can bitmask without worrying about bytes slices.
	entropyInt := new(big.Int).SetBytes(entropy)

	// Slice to hold words in.
	words := make([]string, sentenceLength)

	// Throw away big.Int for AND masking.
	word := big.NewInt(0)

	for i := sentenceLength - 1; i >= 0; i-- {
		// Get 11 right most bits and bitshift 11 to the right for next time.
		word.And(entropyInt, last11BitsMask)
		entropyInt.Div(entropyInt, shift11BitsMask)

		// Get the bytes representing the 11 bits as a 2 byte slice.
		wordBytes := padByteSlice(word.Bytes(), 2)

		// Convert bytes to an index and add that word to the list.
		words[i] = wordList[binary.BigEndian.Uint16(wordBytes)]
	}

	return strings.Join(words, " "), nil
}
1162755
func CryptoRandomHex(len int) (string, error) {
	randomBytes := make([]byte, len)
	if _, err := rand.Reader.Read(randomBytes); err != nil {
		return "", trace.Wrap(err)
	}
	return hex.EncodeToString(randomBytes), nil
}