Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Turbo码先生
redis
提交
b2868c7b
R
redis
项目概览
Turbo码先生
/
redis
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
redis
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
b2868c7b
编写于
4月 05, 2018
作者:
A
antirez
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Modules API: RM_GetRandomBytes() / GetRandomHexChars().
上级
c7558288
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
71 addition
and
60 deletion
+71
-60
src/module.c
src/module.c
+21
-0
src/redismodule.h
src/redismodule.h
+5
-0
src/server.h
src/server.h
+2
-1
src/util.c
src/util.c
+43
-59
未找到文件。
src/module.c
浏览文件 @
b2868c7b
...
...
@@ -4183,6 +4183,25 @@ int RM_GetTimerInfo(RedisModuleCtx *ctx, RedisModuleTimerID id, uint64_t *remain
return
REDISMODULE_OK
;
}
/* --------------------------------------------------------------------------
* Modules utility APIs
* -------------------------------------------------------------------------- */
/* Return random bytes using SHA1 in counter mode with a /dev/urandom
* initialized seed. This function is fast so can be used to generate
* many bytes without any effect on the operating system entropy pool.
* Currently this function is not thread safe. */
void
RM_GetRandomBytes
(
unsigned
char
*
dst
,
size_t
len
)
{
getRandomBytes
(
dst
,
len
);
}
/* Like RedisModule_GetRandomBytes() but instead of setting the string to
* random bytes the string is set to random characters in the in the
* hex charset [0-9a-f]. */
void
RM_GetRandomHexChars
(
char
*
dst
,
size_t
len
)
{
getRandomHexChars
(
dst
,
len
);
}
/* --------------------------------------------------------------------------
* Modules API internals
* -------------------------------------------------------------------------- */
...
...
@@ -4575,4 +4594,6 @@ void moduleRegisterCoreAPI(void) {
REGISTER_API
(
GetTimerInfo
);
REGISTER_API
(
GetMyClusterID
);
REGISTER_API
(
GetClusterSize
);
REGISTER_API
(
GetRandomBytes
);
REGISTER_API
(
GetRandomHexChars
);
}
src/redismodule.h
浏览文件 @
b2868c7b
...
...
@@ -278,6 +278,9 @@ int REDISMODULE_API_FUNC(RedisModule_StopTimer)(RedisModuleCtx *ctx, RedisModule
int
REDISMODULE_API_FUNC
(
RedisModule_GetTimerInfo
)(
RedisModuleCtx
*
ctx
,
RedisModuleTimerID
id
,
uint64_t
*
remaining
,
void
**
data
);
const
char
*
REDISMODULE_API_FUNC
(
RedisModule_GetMyClusterID
)(
void
);
size_t
REDISMODULE_API_FUNC
(
RedisModule_GetClusterSize
)(
void
);
void
REDISMODULE_API_FUNC
(
RedisModule_GetRandomBytes
)(
unsigned
char
*
dst
,
size_t
len
);
void
REDISMODULE_API_FUNC
(
RedisModule_GetRandomHexChars
)(
char
*
dst
,
size_t
len
);
/* Experimental APIs */
#ifdef REDISMODULE_EXPERIMENTAL_API
...
...
@@ -411,6 +414,8 @@ static int RedisModule_Init(RedisModuleCtx *ctx, const char *name, int ver, int
REDISMODULE_GET_API
(
GetTimerInfo
);
REDISMODULE_GET_API
(
GetMyClusterID
);
REDISMODULE_GET_API
(
GetClusterSize
);
REDISMODULE_GET_API
(
GetRandomBytes
);
REDISMODULE_GET_API
(
GetRandomHexChars
);
#ifdef REDISMODULE_EXPERIMENTAL_API
REDISMODULE_GET_API
(
GetThreadSafeContext
);
...
...
src/server.h
浏览文件 @
b2868c7b
...
...
@@ -1366,7 +1366,8 @@ void moduleNotifyKeyspaceEvent(int type, const char *event, robj *key, int dbid)
/* Utils */
long
long
ustime
(
void
);
long
long
mstime
(
void
);
void
getRandomHexChars
(
char
*
p
,
unsigned
int
len
);
void
getRandomHexChars
(
char
*
p
,
size_t
len
);
void
getRandomBytes
(
unsigned
char
*
p
,
size_t
len
);
uint64_t
crc64
(
uint64_t
crc
,
const
unsigned
char
*
s
,
uint64_t
l
);
void
exitFromChild
(
int
retcode
);
size_t
redisPopcount
(
void
*
s
,
long
count
);
...
...
src/util.c
浏览文件 @
b2868c7b
...
...
@@ -536,14 +536,12 @@ int ld2string(char *buf, size_t len, long double value, int humanfriendly) {
return
l
;
}
/* Generate the Redis "Run ID", a SHA1-sized random number that identifies a
* given execution of Redis, so that if you are talking with an instance
* having run_id == A, and you reconnect and it has run_id == B, you can be
* sure that it is either a different instance or it was restarted. */
void
getRandomHexChars
(
char
*
p
,
unsigned
int
len
)
{
char
*
charset
=
"0123456789abcdef"
;
unsigned
int
j
;
/* Get random bytes, attempts to get an initial seed from /dev/urandom and
* the uses a one way hash function in counter mode to generate a random
* stream. However if /dev/urandom is not available, a weaker seed is used.
*
* This function is not thread safe, since the state is global. */
void
getRandomBytes
(
unsigned
char
*
p
,
size_t
len
)
{
/* Global state. */
static
int
seed_initialized
=
0
;
static
unsigned
char
seed
[
20
];
/* The SHA1 seed, from /dev/urandom. */
...
...
@@ -555,64 +553,50 @@ void getRandomHexChars(char *p, unsigned int len) {
* function we just need non-colliding strings, there are no
* cryptographic security needs. */
FILE
*
fp
=
fopen
(
"/dev/urandom"
,
"r"
);
if
(
fp
&&
fread
(
seed
,
sizeof
(
seed
),
1
,
fp
)
==
1
)
if
(
fp
==
NULL
||
fread
(
seed
,
sizeof
(
seed
),
1
,
fp
)
!=
1
)
{
/* Revert to a weaker seed, and in this case reseed again
* at every call.*/
for
(
unsigned
int
j
=
0
;
j
<
sizeof
(
seed
);
j
++
)
{
struct
timeval
tv
;
gettimeofday
(
&
tv
,
NULL
);
pid_t
pid
=
getpid
();
seed
[
j
]
=
tv
.
tv_sec
^
tv
.
tv_usec
^
pid
^
(
long
)
fp
;
}
}
else
{
seed_initialized
=
1
;
}
if
(
fp
)
fclose
(
fp
);
}
if
(
seed_initialized
)
{
while
(
len
)
{
unsigned
char
digest
[
20
];
SHA1_CTX
ctx
;
unsigned
int
copylen
=
len
>
20
?
20
:
len
;
SHA1Init
(
&
ctx
);
SHA1Update
(
&
ctx
,
seed
,
sizeof
(
seed
));
SHA1Update
(
&
ctx
,
(
unsigned
char
*
)
&
counter
,
sizeof
(
counter
));
SHA1Final
(
digest
,
&
ctx
);
counter
++
;
memcpy
(
p
,
digest
,
copylen
);
/* Convert to hex digits. */
for
(
j
=
0
;
j
<
copylen
;
j
++
)
p
[
j
]
=
charset
[
p
[
j
]
&
0x0F
];
len
-=
copylen
;
p
+=
copylen
;
}
}
else
{
/* If we can't read from /dev/urandom, do some reasonable effort
* in order to create some entropy, since this function is used to
* generate run_id and cluster instance IDs */
char
*
x
=
p
;
unsigned
int
l
=
len
;
struct
timeval
tv
;
pid_t
pid
=
getpid
();
/* Use time and PID to fill the initial array. */
gettimeofday
(
&
tv
,
NULL
);
if
(
l
>=
sizeof
(
tv
.
tv_usec
))
{
memcpy
(
x
,
&
tv
.
tv_usec
,
sizeof
(
tv
.
tv_usec
));
l
-=
sizeof
(
tv
.
tv_usec
);
x
+=
sizeof
(
tv
.
tv_usec
);
}
if
(
l
>=
sizeof
(
tv
.
tv_sec
))
{
memcpy
(
x
,
&
tv
.
tv_sec
,
sizeof
(
tv
.
tv_sec
));
l
-=
sizeof
(
tv
.
tv_sec
);
x
+=
sizeof
(
tv
.
tv_sec
);
}
if
(
l
>=
sizeof
(
pid
))
{
memcpy
(
x
,
&
pid
,
sizeof
(
pid
));
l
-=
sizeof
(
pid
);
x
+=
sizeof
(
pid
);
}
/* Finally xor it with rand() output, that was already seeded with
* time() at startup, and convert to hex digits. */
for
(
j
=
0
;
j
<
len
;
j
++
)
{
p
[
j
]
^=
rand
();
p
[
j
]
=
charset
[
p
[
j
]
&
0x0F
];
}
while
(
len
)
{
unsigned
char
digest
[
20
];
SHA1_CTX
ctx
;
unsigned
int
copylen
=
len
>
20
?
20
:
len
;
SHA1Init
(
&
ctx
);
SHA1Update
(
&
ctx
,
seed
,
sizeof
(
seed
));
SHA1Update
(
&
ctx
,
(
unsigned
char
*
)
&
counter
,
sizeof
(
counter
));
SHA1Final
(
digest
,
&
ctx
);
counter
++
;
memcpy
(
p
,
digest
,
copylen
);
len
-=
copylen
;
p
+=
copylen
;
}
}
/* Generate the Redis "Run ID", a SHA1-sized random number that identifies a
* given execution of Redis, so that if you are talking with an instance
* having run_id == A, and you reconnect and it has run_id == B, you can be
* sure that it is either a different instance or it was restarted. */
void
getRandomHexChars
(
char
*
p
,
size_t
len
)
{
char
*
charset
=
"0123456789abcdef"
;
size_t
j
;
getRandomBytes
((
unsigned
char
*
)
p
,
len
);
for
(
j
=
0
;
j
<
len
;
j
++
)
p
[
j
]
=
charset
[
p
[
j
]
&
0x0F
];
}
/* Given the filename, return the absolute path as an SDS string, or NULL
* if it fails for some reason. Note that "filename" may be an absolute path
* already, this will be detected and handled correctly.
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录