Conversion of secret to buffer for HOTP

@module otplib-core/hotpSecret
@param {string} secret - your secret that is used to generate the token
@param {string} options.encoding - the encoding of secret
@return {object}
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);
}
780854
function Tokens (options) {
  if (!(this instanceof Tokens)) {
    return new Tokens(options)
  }

  var opts = options || {}

  var saltLength = opts.saltLength !== undefined
    ? opts.saltLength
    : 8

  if (typeof saltLength !== 'number' || !isFinite(saltLength) || saltLength < 1) {
    throw new TypeError('option saltLength must be finite number > 1')
  }

  var secretLength = opts.secretLength !== undefined
    ? opts.secretLength
    : 18

  if (typeof secretLength !== 'number' || !isFinite(secretLength) || secretLength < 1) {
    throw new TypeError('option secretLength must be finite number > 1')
  }

  this.saltLength = saltLength
  this.secretLength = secretLength
}
1141646
public static function decode($input, $secret = null) {
        // no input?
        if(!$input)
            return false;

        // decode
        $decoded = self::base64decode($input);
        if(!$decoded)
            return false;

        // output
        return self::xor($decoded, self::secret($secret));
    }
391129
public static function init($secret, $options = array()) {
    self::assert($secret, "Segment::init() requires secret");
    self::$client = new Segment_Client($secret, $options);
  }
417508
public static Token parseToken(byte[] secret, String token) {
        Token tk = new Token();
        if (S.blank(token)) return tk;
        String s = "";
        try {
            s = Crypto.decryptAES(token, secret);
        } catch (Exception e) {
            return tk;
        }
        String[] sa = s.split("\\|");
        if (sa.length < 2) return tk;
        tk.id = sa[0];
        try {
            tk.due = Long.parseLong(sa[1]);
            if (tk.expired()) {
                return tk;
            }
        } catch (Exception e) {
            tk.due = $.ms() - 1000 * 60 * 60 * 24;
            return tk;
        }
        if (sa.length > 2) {
            sa = Arrays.copyOfRange(sa, 2, sa.length);
            tk.payload.addAll(C.listOf(sa));
        }
        return tk;
    }
1318516
public String generate(byte[] secret) {
    String hp = header.encode() + "." + payload.encode();
    return hp + "." + new String(
      SignerFactory.get(header.algorithm).sign(hp.getBytes(StandardCharsets.UTF_8), secret),
      StandardCharsets.UTF_8
    );
  }
1342463
protected function _base32Encode($secret, $padding = true)
    {
        if (empty($secret)) return '';

        $base32chars = $this->_getBase32LookupTable();

        $secret = str_split($secret);
        $binaryString = "";
        for ($i = 0; $i < count($secret); $i++) {
            $binaryString .= str_pad(base_convert(ord($secret[$i]), 10, 2), 8, '0', STR_PAD_LEFT);
        }
        $fiveBitBinaryArray = str_split($binaryString, 5);
        $base32 = "";
        $i = 0;
        while ($i < count($fiveBitBinaryArray)) {
            $base32 .= $base32chars[base_convert(str_pad($fiveBitBinaryArray[$i], 5, '0'), 2, 10)];
            $i++;
        }
        if ($padding && ($x = strlen($binaryString) % 40) != 0) {
            if ($x == 8) $base32 .= str_repeat($base32chars[32], 6);
            elseif ($x == 16) $base32 .= str_repeat($base32chars[32], 4);
            elseif ($x == 24) $base32 .= str_repeat($base32chars[32], 3);
            elseif ($x == 32) $base32 .= $base32chars[32];
        }
        return $base32;
    }
102722
@SneakyThrows
    public static Key generateJsonWebKey(final String secret) {
        val keys = new HashMap<String, Object>(2);
        keys.put("kty", "oct");
        keys.put(EncodingUtils.JSON_WEB_KEY, secret);
        val jwk = JsonWebKey.Factory.newJwk(keys);
        return jwk.getKey();
    }
839485
function encrypt(key, iv, data) {
    if(!Buffer.isBuffer(key)) {
        assert(typeof key === 'string');
        key = Buffer.from(key, 'binary')
    }

    if(!Buffer.isBuffer(iv)) {
        assert(typeof iv === 'string');
        iv = Buffer.from(iv, 'binary')
    }

    if(!Buffer.isBuffer(data)) {
        assert(typeof data === 'string');
        data = Buffer.from(data, 'binary')
    }

    var crypted = aes.encipher(data, key, iv);

    crypted = Buffer.from(crypted, 'binary');
    
    let ret = Buffer.alloc(crypted.length*2);
    for(let i = 0; i < crypted.length; i++){
        ret[2*i] = (crypted[i] >> 4) + 97;
        ret[2*i+1] = (crypted[i] & 0x0F) + 97;
    }
    return ret.toString('binary');
}
106254
function cookiee(secret, opt) {

  if (!secret) {
    throw new TypeError('secret required');
  }
  var options = opt || Object.create(null);
  var my = {
    secret: Buffer.isBuffer(secret) ? secret : new Buffer(secret),
    cipher: String(options.cipher || 'arc4'),
    cookie: String(options.cookie || 'vault'),
    domain: String(options.domain || ''),
    path: String(options.path || '/'),
    maxAge: Number(options.maxAge) || 1000 * 3600 * 24 * 365,
    httpOnly: Boolean(options.httpOnly),
    secure: Boolean(options.secure),
    signed: Boolean(options.signed),
    encoding: String(options.encoding || 'hex'),
    extra: Array.isArray(options.extra) === true ? options.extra : []
  };

  return Boolean(options.signed) ? new Signed(my) : new Normal(my);
}
379620
@Nonnull
    public static Secret fromString(@CheckForNull String data) {
        data = Util.fixNull(data);
        Secret s = decrypt(data);
        if(s==null) s=new Secret(data);
        return s;
    }
67167
function decodeBuffer(buf, options, cb) {
  if (typeof options === 'function') {
    cb = options;
    options = {};
  }

  try {
    buf = bufferUtils.toUint8Array(buf);

    if (_hasWorker.hasWorker) {
      const wr = require('webworkify')(_decodeWorker2.default);

      wr.onmessage = ({ data: msg }) => {
        const err = msg.err ? new Error(msg.err) : undefined;
        cb(err, msg.result);
      };

      const msg = {
        buf: buf,
        options: options
      };

      if (options.transferable) {
        wr.postMessage(msg, [buf]);
      } else {
        wr.postMessage(msg);
      }
    } else {
      (0, _decode2.default)(buf, options, cb);
    }
  } catch (err) {
    cb(err);
  }
}
1318744
@Deprecated
    public static Token parseToken(String secret, String token) {
        return parseToken(secret.getBytes(Charsets.UTF_8), token);
    }
894165
function getKeys (secret) {
	var hash = crypto.createHash('sha256').update(secret, 'utf8').digest();
	var keypair = naclInstance.crypto_sign_keypair_from_seed(hash);

	return {
		publicKey : Buffer.from(keypair.signPk).toString('hex'),
		privateKey : Buffer.from(keypair.signSk).toString('hex')
	};
}
639924
function createDapp (secret, secondSecret, options) {
	var keys = crypto.getKeys(secret);

	var transaction = {
		type: 5,
		amount: 0,
		fee: constants.fees.dapp,
		recipientId: null,
		senderPublicKey: keys.publicKey,
		timestamp: slots.getTime(),
		asset: {
			dapp: {
				category: options.category,
				name: options.name,
				description: options.description,
				tags: options.tags,
				type: options.type,
				link: options.link,
				icon: options.icon
			}
		}
	};

	crypto.sign(transaction, keys);

	if (secondSecret) {
		var secondKeys = crypto.getKeys(secondSecret);
		crypto.secondSign(transaction, secondKeys);
	}

	transaction.id = crypto.getId(transaction);
	return transaction;
}
551575
function IEncrypt(options){
	options = options || {};
	var key = options.key ? new Buffer(options.key) : crypto.randomBytes(32);
	var debug = options.debug || false;
	
	/**
	 * Encryption method.
	 * @param data - What to encrypt it can be an object, string or number.
	 * @return {String} the encrypted text or null on fail
	 */
	this.encrypt = function(data){
		try{
			//serialize
			var original = serialize(data);
			
			//if serialization failed throw an error
			if(!original) throw new Error('Invalid type of object given for encryption');
			
			//initialization vector
			var vector = new Buffer(crypto.randomBytes(16));
			
			//new cipher
			var cipher = crypto.createCipheriv('aes256', key, vector);
			
			//the encrypted text
			var encrypted = cipher.update(original,'utf8','base64') + cipher.final('base64');
			
			//the signature for the combination of encrypted text and initialization vector
			var hash = this.sign(encrypted+vector.toString('base64'));
			
			//return token ecrypted.vector.hash
			return encrypted + "." + vector.toString('base64') + "." + hash;
		}
		catch(e){
			if(debug) console.error('An error occured while encrypting : %s',e.stack);
			return null;
		}
	};
	
	/**
	 * Decryption method.
	 * @param text {String} to encrypt
	 * @returns {Object} || {String} || {Number} || null on fail
	 */
	this.decrypt = function(text){
		var decipher,
			original,
			t,
			encrypted,
			vector,
			hash,
			new_hash;
		try{
			// split text and save each value (encrypted text,vector,hash)
			t=text.split(".");
			encrypted = t[0];
			vector = new Buffer(t[1], 'base64');
			hash = t[2];
			
			//check data integrity by validating signature 
			if(!this.validate(encrypted+t[1], hash)){
				throw new Error('Could not verify signature');
			}
			
			//new decipher
			decipher = crypto.createDecipheriv('aes256', key, vector);
		    
			//return the resulted object
			original = decipher.update(encrypted, 'base64', 'utf8') + decipher.final('utf8');
			try{
				return JSON.parse(original);
			}
			catch(e){
				return (!isNaN(parseFloat(original)) && isFinite(original) ? parseFloat(original) : original);
			}
		}
		catch(e){
			if(debug) console.error('An error occured while decrypting : %s',e.stack);
			return null;
		}
	};
	
	/**
	 * Produce a signature for the given object
	 * @param data {Object} || {String} || {Number}
	 * @param format {string} The format of the signature - Default is 'base64'
	 * @returns {String} the signature or null on fail
	 */
	this.sign = function(data, format){
		try{
			// Serialize data
			var serialized = serialize(data);
			
			// If not serialized properly throw an Error
			if(!serialized) throw new Error('Cannot serialize given type of object');
			
			// Set formating
			format = format || 'base64';
			
			// Generate and return HMAC
			return crypto.createHmac('sha256', key).update(serialized).digest(format);
		}
		catch(e){
			if(debug) console.error('An error occured while signing : %s',e.stack);
			return null;
		}
	}
	
	/**
	 * Validate data signature
	 * @param data  {Object} || {String} || {Number}
	 * @param signature {String} the produced signature for the data
	 * @param format {String} format of the signature. Default is 'base64'
	 * @return {Boolean}
	 */
	this.validate = function(data, signature, format){
		var new_sign = this.sign(data,format);
		return new_sign?compare(signature,new_sign):false;
	}
	
	/**
	 * Private serialization method
	 * @private
	 * @param data {Object} || {String} || {Number}
	 * @returns {String} or null on fail
	 */
	var serialize = function(data){
		return typeof data === 'object' 
			? JSON.stringify(data) 
			: (typeof data === 'string' || typeof data === 'number' ? String(data) : null);
	}
}
722723
public static String encode(final String secret, final int relocate)
	{
		final StringBuffer sb = new StringBuffer();
		final char[] encrypt = secret.toCharArray();
		final int arraylength = encrypt.length;
		for (int i = 0; i < arraylength; i++)
		{
			final char a = (char)(encrypt[i] + relocate);
			sb.append(a);
		}
		return sb.toString().trim();
	}