Return a random 32 byte secret except the 0 secret since it's not accepted in the contracts
8653
def random_secret() -> Secret:
    
    while True:
        secret = os.urandom(32)
        if secret != constants.EMPTY_HASH:
            return Secret(secret)
876665
def secret_key_bytes(self):
        ''' 

        '''
        if not hasattr(self, '_secret_key_bytes'):
            key = self.secret_key()
            if key is None:
                self._secret_key_bytes = None
            else:
                self._secret_key_bytes = codecs.encode(key, "utf-8")
        return self._secret_key_bytes
1177768
function signHMAC(token, random) {
    var hmac = createHmac('sha256', random);
    let sig = hmac.update(token).digest('hex');
    return sig;
}
211518
function random_bytes(count) {
        var bs = new Uint8Array(count);
        if (typeof (window.crypto) !== "undefined") {
          if (typeof (window.crypto.getRandomValues) !== "undefined") {
            window.crypto.getRandomValues(bs);
            return bs;
          }
        }
        if (typeof (window.msCrypto) !== "undefined") {
          if (typeof (window.msCrypto.getRandomValues) !== "undefined") {
            window.msCrypto.getRandomValues(bs);
            return bs;
          }
        }
        throw {
          message: "No suitable random number generator found!"
        };
      }
318219
def from_b32key(b32_key, state=None):
    ''''''
    try:
        lenient_b32decode(b32_key)
    except TypeError:
        raise ValueError('invalid base32 value')
    return GoogleAuthenticator('otpauth://totp/xxx?%s' %
            urlencode({'secret': b32_key}), state=state)
341096
def tokhex(length=10, urlsafe=False):
    
    if urlsafe is True:
        return secrets.token_urlsafe(length)
    return secrets.token_hex(length)
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
522473
def sign_nonce(key, nonce):
    ''' '''
    # HMAC.new(key, msg='', digestmod=None)
    return Cryptodome.Hash.HMAC.new(key, nonce, digestmod=Cryptodome.Hash.SHA256).digest()
1021520
def rfc6979_ecdsa_nonce(hash, privkey)
      raise ArgumentError, "Hash must be 32 bytes long" if hash.bytesize != 32
      raise ArgumentError, "Private key must be 32 bytes long" if privkey.bytesize != 32

      autorelease do |pool|
        order = self.group_order

        # Step 3.2.a. hash = H(message). Already performed by the caller.

        # Step 3.2.b. V = 0x01 0x01 0x01 ... 0x01 (32 bytes equal 0x01)
        v = "\x01".b*32

        # Step 3.2.c. K = 0x00 0x00 0x00 ... 0x00 (32 bytes equal 0x00)
        k = "\x00".b*32

        # Step 3.2.d. K = HMAC-SHA256(key: K, data: V || 0x00 || int2octets(privkey) || bits2octets(hash))
        h1 = pool.new_bn(hash)
        BN_div(nil, h1, h1, order, pool.bn_ctx) # h1 = h1 % order
        h1data = data_from_bn(h1, min_length: 32)
        k = BTC.hmac_sha256(key: k, data: v + "\x00".b + privkey + h1data)

        # Step 3.2.e. V = HMAC-SHA256(key: K, data: V)
        v = BTC.hmac_sha256(key: k, data: v)

        # Step 3.2.f. K = HMAC-SHA256(key: K, data: V || 0x01 || int2octets(privkey) || bits2octets(hash))
        k = BTC.hmac_sha256(key: k, data: v + "\x01".b + privkey + h1data)

        # Step 3.2.g. V = HMAC-SHA256(key: K, data: V)
        v = BTC.hmac_sha256(key: k, data: v)

        # Step 3.2.h.
        zero32 = "\x00".b*32
        10000.times do
          t = BTC.hmac_sha256(key: k, data: v)
          tn = pool.new_bn(t)
          if BN_cmp(tn, order) < 0
            nonce = data_from_bn(tn, min_length: 32)
            if nonce != zero32
              return nonce
            end
          end
          # Note: the probability of not succeeding at the first try is about 2^-127.
          k = BTC.hmac_sha256(key: k, data: v + zero32)
          v = BTC.hmac_sha256(key: k, data: v)
        end
        # we generated 10000 numbers, none of them is good -> fail.
        raise "Cannot find any good ECDSA nonce after 10000 iterations of RFC6979."
      end
    end
1346884
def generate_secret_key():
    """"""
    import string
    import random

    rng = random.SystemRandom()
    return ''.join(
        rng.choice(string.ascii_letters + string.digits)
        for dummy in range(0, 256)
    )
498583
private static byte[] makeRandomChallenge(final Random random) throws AuthenticationException {
        final byte[] rval = new byte[8];
        synchronized (random) {
            random.nextBytes(rval);
        }
        return rval;
    }
164131
def _create_secret(self, length=12):
        
        # Charset will have 64 +- characters
        charset = string.digits + string.ascii_letters + '+-'
        return "".join(random.SystemRandom().choice(charset) for _ in
                range(length))
765398
function hotpSecret(secret, options) {
  if (typeof options.encoding !== 'string') {
    throw new Error('Expecting options.encoding to be a string');
  }

  return Buffer.from(secret, options.encoding);
}
933296
def signature(secret, parts):
    
    if not isinstance(secret, six.binary_type):
        secret = secret.encode('utf-8')
    newparts = []
    for part in parts:
        if not isinstance(part, six.binary_type):
            part = part.encode('utf-8')
        newparts.append(part)
    parts = newparts
    if sys.version_info >= (2, 5):
        csum = hmac.new(secret, digestmod=hashlib.sha1)
    else:
        csum = hmac.new(secret, digestmod=sha)

    for part in parts:
        csum.update(part)

    return csum.hexdigest()
291564
def encrypt(self, value):
        """"""
        timestamp = str(int(time.time()))
        value = base64.b64encode(value.encode(self.encoding))
        signature = create_signature(self.secret, value + timestamp.encode(),
                                     encoding=self.encoding)
        return "|".join([value.decode(self.encoding), timestamp, signature])
1055654
def make_lock_securely(email = None, warn_only = False):
    ""
    email = email or input("Please provide email address: ")
    while True:
        passphrase = getpass.getpass("Please type a secure passphrase (with spaces): ")
        ok, score = check_passphrase(passphrase, email)
        if ok: break
        print("Insufficiently strong passphrase; has {entropy} bits of entropy, could be broken in {crack_time_display}".format(**score))
        if warn_only: break
        print("Suggestion:", make_random_phrase(email))
    key = crypto.UserLock.from_passphrase(email, passphrase)
    return key
473925
def generate_secret(length=32):
  
  alphabet = string.ascii_letters + string.digits
  random_bytes = os.urandom(length)
  indices = [int(len(alphabet) * (ord(byte) / 256.0)) for byte in random_bytes]
  return "".join([alphabet[index] for index in indices])