Derive a 32-bit b64-encoded Fernet key from arbitrary input key.
180607
def derive_fernet_key(input_key):
    """"""
    hkdf = HKDF(
        algorithm=hashes.SHA256(),
        length=32,
        salt=salt,
        info=info,
        backend=backend,
    )
    return base64.urlsafe_b64encode(hkdf.derive(force_bytes(input_key)))
1202455
private int[][] generateWorkingKey(byte[] key, boolean forEncryption) {
        int keyLen = key.length;
        if (keyLen < 16 || keyLen > 32 || (keyLen & 7) != 0) {
            throw new IllegalArgumentException("Key length not 128/192/256 bits.");
        }

        int KC = keyLen >>> 2;
        ROUNDS = KC + 6;  // This is not always true for the generalized Rijndael that allows larger block sizes
        int[][] W = new int[ROUNDS + 1][4];   // 4 words in a block

        switch (KC) {
            case 4: {
                int t0 = Pack.littleEndianToInt(key, 0);
                W[0][0] = t0;
                int t1 = Pack.littleEndianToInt(key, 4);
                W[0][1] = t1;
                int t2 = Pack.littleEndianToInt(key, 8);
                W[0][2] = t2;
                int t3 = Pack.littleEndianToInt(key, 12);
                W[0][3] = t3;

                for (int i = 1; i <= 10; ++i) {
                    int u = subWord(shift(t3, 8)) ^ rcon[i - 1];
                    t0 ^= u;
                    W[i][0] = t0;
                    t1 ^= t0;
                    W[i][1] = t1;
                    t2 ^= t1;
                    W[i][2] = t2;
                    t3 ^= t2;
                    W[i][3] = t3;
                }

                break;
            }
            case 6: {
                int t0 = Pack.littleEndianToInt(key, 0);
                W[0][0] = t0;
                int t1 = Pack.littleEndianToInt(key, 4);
                W[0][1] = t1;
                int t2 = Pack.littleEndianToInt(key, 8);
                W[0][2] = t2;
                int t3 = Pack.littleEndianToInt(key, 12);
                W[0][3] = t3;
                int t4 = Pack.littleEndianToInt(key, 16);
                W[1][0] = t4;
                int t5 = Pack.littleEndianToInt(key, 20);
                W[1][1] = t5;

                int rcon = 1;
                int u = subWord(shift(t5, 8)) ^ rcon;
                rcon <<= 1;
                t0 ^= u;
                W[1][2] = t0;
                t1 ^= t0;
                W[1][3] = t1;
                t2 ^= t1;
                W[2][0] = t2;
                t3 ^= t2;
                W[2][1] = t3;
                t4 ^= t3;
                W[2][2] = t4;
                t5 ^= t4;
                W[2][3] = t5;

                for (int i = 3; i < 12; i += 3) {
                    u = subWord(shift(t5, 8)) ^ rcon;
                    rcon <<= 1;
                    t0 ^= u;
                    W[i][0] = t0;
                    t1 ^= t0;
                    W[i][1] = t1;
                    t2 ^= t1;
                    W[i][2] = t2;
                    t3 ^= t2;
                    W[i][3] = t3;
                    t4 ^= t3;
                    W[i + 1][0] = t4;
                    t5 ^= t4;
                    W[i + 1][1] = t5;
                    u = subWord(shift(t5, 8)) ^ rcon;
                    rcon <<= 1;
                    t0 ^= u;
                    W[i + 1][2] = t0;
                    t1 ^= t0;
                    W[i + 1][3] = t1;
                    t2 ^= t1;
                    W[i + 2][0] = t2;
                    t3 ^= t2;
                    W[i + 2][1] = t3;
                    t4 ^= t3;
                    W[i + 2][2] = t4;
                    t5 ^= t4;
                    W[i + 2][3] = t5;
                }

                u = subWord(shift(t5, 8)) ^ rcon;
                t0 ^= u;
                W[12][0] = t0;
                t1 ^= t0;
                W[12][1] = t1;
                t2 ^= t1;
                W[12][2] = t2;
                t3 ^= t2;
                W[12][3] = t3;

                break;
            }
            case 8: {
                int t0 = Pack.littleEndianToInt(key, 0);
                W[0][0] = t0;
                int t1 = Pack.littleEndianToInt(key, 4);
                W[0][1] = t1;
                int t2 = Pack.littleEndianToInt(key, 8);
                W[0][2] = t2;
                int t3 = Pack.littleEndianToInt(key, 12);
                W[0][3] = t3;
                int t4 = Pack.littleEndianToInt(key, 16);
                W[1][0] = t4;
                int t5 = Pack.littleEndianToInt(key, 20);
                W[1][1] = t5;
                int t6 = Pack.littleEndianToInt(key, 24);
                W[1][2] = t6;
                int t7 = Pack.littleEndianToInt(key, 28);
                W[1][3] = t7;

                int u, rcon = 1;

                for (int i = 2; i < 14; i += 2) {
                    u = subWord(shift(t7, 8)) ^ rcon;
                    rcon <<= 1;
                    t0 ^= u;
                    W[i][0] = t0;
                    t1 ^= t0;
                    W[i][1] = t1;
                    t2 ^= t1;
                    W[i][2] = t2;
                    t3 ^= t2;
                    W[i][3] = t3;
                    u = subWord(t3);
                    t4 ^= u;
                    W[i + 1][0] = t4;
                    t5 ^= t4;
                    W[i + 1][1] = t5;
                    t6 ^= t5;
                    W[i + 1][2] = t6;
                    t7 ^= t6;
                    W[i + 1][3] = t7;
                }

                u = subWord(shift(t7, 8)) ^ rcon;
                t0 ^= u;
                W[14][0] = t0;
                t1 ^= t0;
                W[14][1] = t1;
                t2 ^= t1;
                W[14][2] = t2;
                t3 ^= t2;
                W[14][3] = t3;

                break;
            }
            default: {
                throw new IllegalStateException("Should never get here");
            }
        }

        if (!forEncryption) {
            for (int j = 1; j < ROUNDS; j++) {
                for (int i = 0; i < 4; i++) {
                    W[j][i] = inv_mcol(W[j][i]);
                }
            }
        }

        return W;
    }
335960
def i2m(self, pkt, x):
        """"""
        if x is None:
            x = 0
        elif isinstance(x, str):
            return bytes_encode(x)
        return x
15626
def cipher(rkey, pt, Nk=4):
    """"""
    assert Nk in {4, 6, 8}
    Nr = Nk + 6

    rkey = rkey.reshape(4*(Nr+1), 32)
    pt = pt.reshape(128)

    # first round
    state = add_round_key(pt, rkey[0:4])

    for i in range(1, Nr):
        state = sub_bytes(state)
        state = shift_rows(state)
        state = mix_columns(state)
        state = add_round_key(state, rkey[4*i:4*(i+1)])

    # final round
    state = sub_bytes(state)
    state = shift_rows(state)
    state = add_round_key(state, rkey[4*Nr:4*(Nr+1)])

    return state
1103921
function(input,key,size)
		{
			var output = [];
			var block = []; /* the 128 bit block to encode */
			var nbrRounds = this.numberOfRounds(size);
			/* Set the block values, for the block:
			 * a0,0 a0,1 a0,2 a0,3
			 * a1,0 a1,1 a1,2 a1,3
			 * a2,0 a2,1 a2,2 a2,3
			 * a3,0 a3,1 a3,2 a3,3
			 * the mapping order is a0,0 a1,0 a2,0 a3,0 a0,1 a1,1 ... a2,3 a3,3
			 */
			for (var i = 0; i < 4; i++) /* iterate over the columns */
				for (var j = 0; j < 4; j++) /* iterate over the rows */
					block[(i+(j*4))] = input[(i*4)+j];
		
			/* expand the key into an 176, 208, 240 bytes key */
			var expandedKey = this.expandKey(key, size); /* the expanded key */
			/* encrypt the block using the expandedKey */
			block = this.main(block, expandedKey, nbrRounds);
			for (var k = 0; k < 4; k++) /* unmap the block again into the output */
				for (var l = 0; l < 4; l++) /* iterate over the rows */
					output[(k*4)+l] = block[(k+(l*4))];
			return output;
		}
1330730
static KuzIntKey convertKey(byte[] key) {
        if (key.length != 32) {
            throw new RuntimeException("Key might be 32 bytes length");
        }

        KuzIntKey kuz = new KuzIntKey();
        // w128_t c, x, y, z;
        Kuz128 c = new Kuz128(), x = new Kuz128(), y = new Kuz128(), z = new Kuz128();

        for (int i = 0; i < 16; i++) {
            // this will be have to changed for little-endian systems
            // x.b[i] = key[i];
            // y.b[i] = key[i + 16];
            x.getB()[i] = key[i];
            y.getB()[i] = key[i + 16];
        }

        // kuz->k[0].q[0] = x.q[0];
        // kuz->k[0].q[1] = x.q[1];
        // kuz->k[1].q[0] = y.q[0];
        // kuz->k[1].q[1] = y.q[1];
        kuz.getK()[0].set(x);
        kuz.getK()[1].set(y);

        for (int i = 1; i <= 32; i++) {
            // C Value
            // c.q[0] = 0;
            // c.q[1] = 0;
            // c.b[15] = i;		// load round in lsb
            // kuz_l(&c);
            c.setQ(0, 0);
            c.setQ(1, 0);
            c.getB()[15] = (byte) i;

            KuznechikMath.kuz_l(c);

            // z.q[0] = x.q[0] ^ c.q[0];
            // z.q[1] = x.q[1] ^ c.q[1];
            // for (j = 0; j < 16; j++)
            //   z.b[j] = kuz_pi[z.b[j]];
            // kuz_l(&z);
            z.setQ(0, x.getQ(0) ^ c.getQ(0));
            z.setQ(1, x.getQ(1) ^ c.getQ(1));

            for (int j = 0; j < 16; j++) {
                z.getB()[j] = KuznechikTables.kuz_pi[(z.getB()[j] & 0xFF)];
            }

            KuznechikMath.kuz_l(z);

            // z.q[0] ^= y.q[0];
            // z.q[1] ^= y.q[1];
            z.setQ(0, z.getQ(0) ^ y.getQ(0));
            z.setQ(1, z.getQ(1) ^ y.getQ(1));

            // y.q[0] = x.q[0];
            // y.q[1] = x.q[1];
            y.set(x);

            // x.q[0] = z.q[0];
            // x.q[1] = z.q[1];
            x.set(z);

            // if ((i & 7) == 0) {
            //    kuz->k[(i >> 2)].q[0] = x.q[0];
            //    kuz->k[(i >> 2)].q[1] = x.q[1];
            //    kuz->k[(i >> 2) + 1].q[0] = y.q[0];
            //    kuz->k[(i >> 2) + 1].q[1] = y.q[1];
            // }
            if ((i & 7) == 0) {
                kuz.getK()[(i >> 2)].set(x);
                kuz.getK()[(i >> 2) + 1].set(y);
            }
        }

        return kuz;
    }
777991
def gen_key(self, *values):
        """"""
        key = md5()
        KeyGen._recursive_convert(values, key)
        return key.hexdigest()
393182
function (input, signature, b64safeKey, alg) {
  var key = new Buffer( b64url.b64( b64safeKey ), 'base64' )
  var hmac = crypto.createHmac( alg, key ).update( input )
  var inputSignature = b64url.safe( hmac.digest( 'base64' ) )
  return (inputSignature === signature)
}
998685
def cmd_fernet_genkey(writecfg):
    

    key = Fernet.generate_key()
    print(key.decode())

    if writecfg:
        habucfg = loadcfg(environment=False)
        habucfg['FERNET_KEY'] = key.decode()
        with Path('~/.habu.json').expanduser().open('w') as f:
            f.write(json.dumps(habucfg, indent=4, sort_keys=True))
32425
def decrypt(self, encrypted):
        
        fernet = Fernet(self.decryption_cipher_key)
        return fernet.decrypt(encrypted)
1351349
def _key_digest(self, secret_key):
        
        '''
            
        '''
        
        from hashlib import md5, sha256
        from base64 import b64encode
        key_bytes = sha256(secret_key.encode('utf-8')).digest()
        key_b64 = b64encode(key_bytes).decode()
        digest_bytes = md5(key_bytes).digest()
        digest_b64 = b64encode(digest_bytes).decode()
        
        return key_b64, digest_b64
441725
def address_from_public_key(pk_bytes):
    
    final_bytes = bytearray()

    # version
    final_bytes.append(6 << 3)
    # public key
    final_bytes.extend(pk_bytes)
    # checksum
    final_bytes.extend(struct.pack("<H", _crc16_checksum(final_bytes)))

    return base64.b32encode(final_bytes).decode()
737917
def gen_secret(length=64):
    
    charset = string.ascii_letters + string.digits
    return ''.join(random.SystemRandom().choice(charset)
                   for _ in range(length))
62025
function fromBLC64(digits){
    var blc = "";
    for (var d=0, l=digits.length; d<l; ++d){
      var bits = base64Table[digits[d]];
      blc += d === 0 ? bits.slice(bits.indexOf("1")+1) : bits;
    };
    return fromBLC(blc);
  }
1287679
function getPubKeyFromPriKey(priKeyBytes) {
  var ec = new EC('secp256k1');
  var key = ec.keyFromPrivate(priKeyBytes, 'bytes');
  var pubkey = key.getPublic();
  var x = pubkey.x;
  var y = pubkey.y;
  var xHex = x.toString('hex');
  while (xHex.length < 64) {
    xHex = "0" + xHex;
  }
  var yHex = y.toString('hex');
  while (yHex.length < 64) {
    yHex = "0" + yHex;
  }
  var pubkeyHex = "04" + xHex + yHex;
  var pubkeyBytes = hexStr2byteArray(pubkeyHex);
  return pubkeyBytes;
}
100639
def public_key_from_ed25519_key(hex_public_key):
    """"""
    public_key = crypto.Ed25519VerifyingKey(bytes.fromhex(hex_public_key), encoding='bytes')
    return public_key.encode(encoding='base58').decode('utf-8')
292329
def base64_bytes(x):
    """"""
    if six.PY2:
        return base64.decodestring(x)
    return base64.decodebytes(bytes_encode(x))