V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  CodeCodeStudy  ›  全部回复第 2 页 / 共 45 页
回复总数  886
1  2  3  4  5  6  7  8  9  10 ... 45  
还不如买腾讯的电视盒子,把电视当显示器用
不要看红宝书,太厚了,而且过时了,推荐阮一峰的教程,在 https://wangdoc.com/
113 天前
回复了 vfx666 创建的主题 问与答 真有人花钱买 ssl 证书?
花的是老板的钱,心疼什么?
117 天前
回复了 click 创建的主题 问与答 有没有靠谱的 2FA 验证器推荐?
php 的代码,不依赖第三方库

```php
<?php

class TOTP {
private static $base32Map = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';

private static function base32Decode($in) {
$l = strlen($in);
$n = $bs = 0;

for ($i = 0; $i < $l; $i++) {
$n <<= 5;
$n += stripos(self::$base32Map, $in[$i]);
$bs = ($bs + 5) % 8;
@$out .= $bs < 5 ? chr(($n & (255 << $bs)) >> $bs) : null;
}

return $out;
}
public static function getOTP($secret, $digits = 6, $period = 30, $offset = 0) {
if (strlen($secret) < 16 || strlen($secret) % 8 != 0)
return ['err' => 'length of secret must be a multiple of 8, and at least 16 characters'];
if (preg_match('/[^a-z2-7]/i', $secret) === 1)
return ['err' => 'secret contains non-base32 characters'];
$digits = intval($digits);
if ($digits < 6 || $digits > 8)
return ['err' => 'digits must be 6, 7 or 8'];

$seed = self::base32Decode($secret);
$time = str_pad(pack('N', intval($offset + time() / $period)), 8, "\x00", STR_PAD_LEFT);
$hash = hash_hmac('sha1', $time, $seed, false);
$otp = (hexdec(substr($hash, hexdec($hash[39]) * 2, 8)) & 0x7fffffff) % pow(10, $digits);

return ['otp' => sprintf("%'0{$digits}u", $otp)];
}
}

echo TOTP::getOTP('xxx')['otp'];
```
118 天前
回复了 WhiteSJ 创建的主题 程序员 Java 比 web 前端要更好找工作吗?
@privater #20 日志也是后端在代码里打的
119 天前
回复了 WhiteSJ 创建的主题 程序员 Java 比 web 前端要更好找工作吗?
@privater #8 你搞反了吧,公司壮大后,后端的工作更多了,从单体架构到微服务,各种日志系统、监控系统、都要搞,而且随着数据增多,数据库的压力也会越来越大。后端跟前端的最大差别是后端是有状态的,也就是之前累积的数据会对系统有影响,10w 的访问量和 1000w 的访问量的架构是不一样的。
120 天前
回复了 echo0x000001 创建的主题 职场话题 应该向父母隐瞒自己的实际工资吗?
你弟上学,你帮一下是应当的,但是他结婚、买房,就不关你事了
120 天前
回复了 hgg12580 创建的主题 计算机 2024 年了,求推荐静音 Win 笔记本
等年中的 x elite 的笔记本吧
121 天前
回复了 baolinliu442k 创建的主题 Java 实际项目中如何使用线程池
底层类库用的,业务中谨慎使用
124 天前
回复了 zhenruyan 创建的主题 Visual Studio Code 做了个 vscode 的下载 mirror 站
124 天前
回复了 zhenruyan 创建的主题 Visual Studio Code 做了个 vscode 的下载 mirror 站
@zhenruyan 你怎么不更新了,现在是 1.85 了,你的镜像站还是 1.79
@rulerstorm #14 怎么会招不到年轻便宜的,只要 IT 岗位的工资比社会平均工资高一些,自然会有很多人涌进来的
125 天前
回复了 OrangeSinglee 创建的主题 问与答 第一次去女朋友家送什么比较好
橘子,香蕉,葡萄,苹果
今年我 35 岁,有 35 岁歧视,10 年后我 45 岁,不再有 35 岁歧视了,但是会有 45 岁歧视
@loginv2 #29 可以使用两步验证,也就是多一个动态口令,有效期只有 30 秒
133 天前
回复了 spitfireuptown 创建的主题 程序员 搞开源项目,收费卖文档有搞头吗
你这样子还不如把你的项目分成社区版和商业版,社区版的代码和文档完全免费,商业版付费。
@way2create #26 对,旧版本的一样的

https://www.php.net/manual/en/function.rand.php

As of PHP 7.1.0, rand() uses the same random number generator as mt_rand().
https://github.com/php/php-src/blob/master/ext/standard/string.c#L5630

```c
PHPAPI bool php_binary_string_shuffle(const php_random_algo *algo, php_random_status *status, char *str, zend_long len) /* {{{ */
{
int64_t n_elems, rnd_idx, n_left;
char temp;

/* The implementation is stolen from array_data_shuffle */
/* Thus the characteristics of the randomization are the same */
n_elems = len;

if (n_elems <= 1) {
return true;
}

n_left = n_elems;

while (--n_left) {
rnd_idx = algo->range(status, 0, n_left);
if (EG(exception)) {
return false;
}
if (rnd_idx != n_left) {
temp = str[n_left];
str[n_left] = str[rnd_idx];
str[rnd_idx] = temp;
}
}

return true;
}
```

https://github.com/php/php-src/blob/master/ext/random/random.c#L423

```c

PHPAPI zend_long php_mt_rand_range(zend_long min, zend_long max)
{
return php_random_algo_mt19937.range(php_random_default_status(), min, max);
}

```


可以看出,都是调用某个算法的 range 函数,所以 str_shuffle 和 mt_rand 的底层都是相似的,差别可能就是算法不一样
用 random_int 这个函数

random_int(int $min, int $max): int
1  2  3  4  5  6  7  8  9  10 ... 45  
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1260 人在线   最高记录 6543   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 36ms · UTC 18:21 · PVG 02:21 · LAX 11:21 · JFK 14:21
Developed with CodeLauncher
♥ Do have faith in what you're doing.