Phalcon:PHP生成隨機(jī)字符串工具

<?php

namespace Phalcon\Security;

/**
 * Phalcon\Security\Random
 *
 * Secure random number generator class.
 *
 * Provides secure random number generator which is suitable for generating
 * session key in HTTP cookies, etc.
 *
 * It supports following secure random number generators:
 *
 * - random_bytes (PHP 7)
 * - libsodium
 * - openssl, libressl
 * - /dev/urandom
 *
 * `Phalcon\Security\Random` could be mainly useful for:
 *
 * - Key generation (e.g. generation of complicated keys)
 * - Generating random passwords for new user accounts
 * - Encryption systems
 *
 * <code>
 * $random = new \Phalcon\Security\Random();
 *
 * // Random binary string
 * $bytes = $random->bytes();
 *
 * // Random hex string
 * echo $random->hex(10); // a29f470508d5ccb8e289
 * echo $random->hex(10); // 533c2f08d5eee750e64a
 * echo $random->hex(11); // f362ef96cb9ffef150c9cd
 * echo $random->hex(12); // 95469d667475125208be45c4
 * echo $random->hex(13); // 05475e8af4a34f8f743ab48761
 *
 * // Random base62 string
 * echo $random->base62(); // z0RkwHfh8ErDM1xw
 *
 * // Random base64 string
 * echo $random->base64(12); // XfIN81jGGuKkcE1E
 * echo $random->base64(12); // 3rcq39QzGK9fUqh8
 * echo $random->base64();   // DRcfbngL/iOo9hGGvy1TcQ==
 * echo $random->base64(16); // SvdhPcIHDZFad838Bb0Swg==
 *
 * // Random URL-safe base64 string
 * echo $random->base64Safe();           // PcV6jGbJ6vfVw7hfKIFDGA
 * echo $random->base64Safe();           // GD8JojhzSTrqX7Q8J6uug
 * echo $random->base64Safe(8);          // mGyy0evy3ok
 * echo $random->base64Safe(null, true); // DRrAgOFkS4rvRiVHFefcQ==
 *
 * // Random UUID
 * echo $random->uuid(); // db082997-2572-4e2c-a046-5eefe97b1235
 * echo $random->uuid(); // da2aa0e2-b4d0-4e3c-99f5-f5ef62c57fe2
 * echo $random->uuid(); // 75e6b628-c562-4117-bb76-61c4153455a9
 * echo $random->uuid(); // dc446df1-0848-4d05-b501-4af3c220c13d
 *
 * // Random number between 0 and $len
 * echo $random->number(256); // 84
 * echo $random->number(256); // 79
 * echo $random->number(100); // 29
 * echo $random->number(300); // 40
 *
 * // Random base58 string
 * echo $random->base58();   // 4kUgL2pdQMSCQtjE
 * echo $random->base58();   // Umjxqf7ZPwh765yR
 * echo $random->base58(24); // qoXcgmw4A9dys26HaNEdCRj9
 * echo $random->base58(7);  // 774SJD3vgP
 * </code>
 *
 * This class partially borrows SecureRandom library from Ruby
 *
 * @link http://ruby-doc.org/stdlib-2.2.2/libdoc/securerandom/rdoc/SecureRandom.html
 */
class Random
{

    /**
     * Generates a random binary string
     *
     * The `Random::bytes` method returns a string and accepts as input an int
     * representing the length in bytes to be returned.
     *
     * If $len is not specified, 16 is assumed. It may be larger in future.
     * The result may contain any byte: "x00" - "xFF".
     *
     * <code>
     * $random = new \Phalcon\Security\Random();
     *
     * $bytes = $random->bytes();
     * var_dump(bin2hex($bytes));
     * // Possible output: string(32) "00f6c04b144b41fad6a59111c126e1ee"
     * </code>
     *
     * @throws Exception If secure random number generator is not available or unexpected partial read
     * @param int $len
     * @return string
     */
    public function bytes($len = 16) {}

    /**
     * Generates a random hex string
     *
     * If $len is not specified, 16 is assumed. It may be larger in future.
     * The length of the result string is usually greater of $len.
     *
     * <code>
     * $random = new \Phalcon\Security\Random();
     *
     * echo $random->hex(10); // a29f470508d5ccb8e289
     * </code>
     *
     * @throws Exception If secure random number generator is not available or unexpected partial read
     * @param int $len
     * @return string
     */
    public function hex($len = null) {}

    /**
     * Generates a random base58 string
     *
     * If $len is not specified, 16 is assumed. It may be larger in future.
     * The result may contain alphanumeric characters except 0, O, I and l.
     *
     * It is similar to `Phalcon\Security\Random:base64` but has been modified to avoid both non-alphanumeric
     * characters and letters which might look ambiguous when printed.
     *
     * <code>
     * $random = new \Phalcon\Security\Random();
     *
     * echo $random->base58(); // 4kUgL2pdQMSCQtjE
     * </code>
     *
     * @see    \Phalcon\Security\Random:base64
     * @link   https://en.wikipedia.org/wiki/Base58
     * @throws Exception If secure random number generator is not available or unexpected partial read
     * @param int $len
     * @return string
     */
    public function base58($len = null) {}

    /**
     * Generates a random base62 string
     *
     * If $len is not specified, 16 is assumed. It may be larger in future.
     *
     * It is similar to `Phalcon\Security\Random:base58` but has been modified to provide the largest value that can
     * safely be used in URLs without needing to take extra characters into consideration because it is [A-Za-z0-9].
     *
     * <code>
     * $random = new \Phalcon\Security\Random();
     *
     * echo $random->base62(); // z0RkwHfh8ErDM1xw
     * </code>
     *
     * @see    \Phalcon\Security\Random:base58
     * @throws Exception If secure random number generator is not available or unexpected partial read
     * @param int $len
     * @return string
     */
    public function base62($len = null) {}

    /**
     * Generates a random base64 string
     *
     * If $len is not specified, 16 is assumed. It may be larger in future.
     * The length of the result string is usually greater of $len.
     * Size formula: 4 ($len / 3) and this need to be rounded up to a multiple of 4.
     *
     * <code>
     * $random = new \Phalcon\Security\Random();
     *
     * echo $random->base64(12); // 3rcq39QzGK9fUqh8
     * </code>
     *
     * @throws Exception If secure random number generator is not available or unexpected partial read
     * @param int $len
     * @return string
     */
    public function base64($len = null) {}

    /**
     * Generates a random URL-safe base64 string
     *
     * If $len is not specified, 16 is assumed. It may be larger in future.
     * The length of the result string is usually greater of $len.
     *
     * By default, padding is not generated because "=" may be used as a URL delimiter.
     * The result may contain A-Z, a-z, 0-9, "-" and "_". "=" is also used if $padding is true.
     * See RFC 3548 for the definition of URL-safe base64.
     *
     * <code>
     * $random = new \Phalcon\Security\Random();
     *
     * echo $random->base64Safe(); // GD8JojhzSTrqX7Q8J6uug
     * </code>
     *
     * @link https://www.ietf.org/rfc/rfc3548.txt
     * @throws Exception If secure random number generator is not available or unexpected partial read
     * @param int $len
     * @param bool $padding
     * @return string
     */
    public function base64Safe($len = null, $padding = false) {}

    /**
     * Generates a v4 random UUID (Universally Unique IDentifier)
     *
     * The version 4 UUID is purely random (except the version). It doesn't contain meaningful
     * information such as MAC address, time, etc. See RFC 4122 for details of UUID.
     *
     * This algorithm sets the version number (4 bits) as well as two reserved bits.
     * All other bits (the remaining 122 bits) are set using a random or pseudorandom data source.
     * Version 4 UUIDs have the form xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx where x is any hexadecimal
     * digit and y is one of 8, 9, A, or B (e.g., f47ac10b-58cc-4372-a567-0e02b2c3d479).
     *
     * <code>
     * $random = new \Phalcon\Security\Random();
     *
     * echo $random->uuid(); // 1378c906-64bb-4f81-a8d6-4ae1bfcdec22
     * </code>
     *
     * @link https://www.ietf.org/rfc/rfc4122.txt
     * @throws Exception If secure random number generator is not available or unexpected partial read
     * @return string
     */
    public function uuid() {}

    /**
     * Generates a random number between 0 and $len
     *
     * Returns an integer: 0 <= result <= $len.
     *
     * <code>
     * $random = new \Phalcon\Security\Random();
     *
     * echo $random->number(16); // 8
     * </code>
     *
     * @throws Exception If secure random number generator is not available, unexpected partial read or $len <= 0
     * @param int $len
     * @return int
     */
    public function number($len) {}

    /**
     * Generates a random string based on the number ($base) of characters ($alphabet).
     *
     * If $n is not specified, 16 is assumed. It may be larger in future.
     *
     * @throws Exception If secure random number generator is not available or unexpected partial read
     * @param string $alphabet
     * @param int $base
     * @param mixed $n
     * @return string
     */
    protected function base($alphabet, $base, $n = null) {}

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末弟晚,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子板乙,更是在濱河造成了極大的恐慌弹渔,老刑警劉巖展姐,帶你破解...
    沈念sama閱讀 206,602評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異烧董,居然都是意外死亡督暂,警方通過(guò)查閱死者的電腦和手機(jī)辜限,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,442評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門皇拣,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人薄嫡,你說(shuō)我怎么就攤上這事氧急。” “怎么了毫深?”我有些...
    開(kāi)封第一講書(shū)人閱讀 152,878評(píng)論 0 344
  • 文/不壞的土叔 我叫張陵吩坝,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我哑蔫,道長(zhǎng)钉寝,這世上最難降的妖魔是什么弧呐? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 55,306評(píng)論 1 279
  • 正文 為了忘掉前任,我火速辦了婚禮嵌纲,結(jié)果婚禮上俘枫,老公的妹妹穿的比我還像新娘。我一直安慰自己逮走,他們只是感情好鸠蚪,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,330評(píng)論 5 373
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著师溅,像睡著了一般茅信。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上墓臭,一...
    開(kāi)封第一講書(shū)人閱讀 49,071評(píng)論 1 285
  • 那天蘸鲸,我揣著相機(jī)與錄音,去河邊找鬼窿锉。 笑死棚贾,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的榆综。 我是一名探鬼主播妙痹,決...
    沈念sama閱讀 38,382評(píng)論 3 400
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼鼻疮!你這毒婦竟也來(lái)了怯伊?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 37,006評(píng)論 0 259
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤判沟,失蹤者是張志新(化名)和其女友劉穎耿芹,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體挪哄,經(jīng)...
    沈念sama閱讀 43,512評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡吧秕,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,965評(píng)論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了迹炼。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片砸彬。...
    茶點(diǎn)故事閱讀 38,094評(píng)論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖斯入,靈堂內(nèi)的尸體忽然破棺而出砂碉,到底是詐尸還是另有隱情,我是刑警寧澤刻两,帶...
    沈念sama閱讀 33,732評(píng)論 4 323
  • 正文 年R本政府宣布增蹭,位于F島的核電站,受9級(jí)特大地震影響磅摹,放射性物質(zhì)發(fā)生泄漏滋迈。R本人自食惡果不足惜霎奢,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,283評(píng)論 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望饼灿。 院中可真熱鬧椰憋,春花似錦、人聲如沸赔退。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,286評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)硕旗。三九已至窗骑,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間漆枚,已是汗流浹背创译。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,512評(píng)論 1 262
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留墙基,地道東北人软族。 一個(gè)月前我還...
    沈念sama閱讀 45,536評(píng)論 2 354
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像残制,于是被迫代替她去往敵國(guó)和親立砸。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,828評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容