Python 中的随机字节字符串

发布于 2024-10-27 17:00:51 字数 101 浏览 2 评论 0原文

我有 buf="\x00\xFF\xFF\xFF\xFF\x00"

如何使 "\xFF\xFF\xFF\xFF" 随机化?

I have buf="\x00\xFF\xFF\xFF\xFF\x00"

How can I make the "\xFF\xFF\xFF\xFF" randomized?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

撧情箌佬 2024-11-03 17:00:51
>>> import os
>>> "\x00"+os.urandom(4)+"\x00"
'\x00!\xc0zK\x00'
>>> import os
>>> "\x00"+os.urandom(4)+"\x00"
'\x00!\xc0zK\x00'
海未深 2024-11-03 17:00:51

获取安全随机字节序列的另一种方法是使用标准库 secrets 模块,该模块自 Python 3.6 起可用。

示例,基于给定的问题:

import secrets
b"\x00" + secrets.token_bytes(4) + b"\x00"

更多信息可以在以下位置找到:
https://docs.python.org/3/library/secrets.html

An alternative way to obtaining a secure random sequence of bytes could be to use the standard library secrets module, available since Python 3.6.

Example, based on the given question:

import secrets
b"\x00" + secrets.token_bytes(4) + b"\x00"

More information can be found at:
https://docs.python.org/3/library/secrets.html

关于从前 2024-11-03 17:00:51
bytearray(random.getrandbits(8) for _ in xrange(size))

比其他解决方案更快,但不加密安全。

bytearray(random.getrandbits(8) for _ in xrange(size))

Faster than other solutions but not cryptographically secure.

も星光 2024-11-03 17:00:51

Python 3.9 添加了新的 random.randbytes方法。此方法生成随机字节:

from random import randbytes

randbytes(4)

输出:

b'\xf3\xf5\xf8\x98'

不过要小心。仅当您不处理密码学时才应使用它。正如文档中所述:

此方法不应用于生成安全令牌。使用 secrets.token_bytes()相反。

Python 3.9 adds a new random.randbytes method. This method generates random bytes:

from random import randbytes

randbytes(4)

Output:

b'\xf3\xf5\xf8\x98'

Be careful though. It should be used only when you are not dealing with cryptography. As stated in the docs:

This method should not be used for generating security tokens. Use secrets.token_bytes() instead.

一世旳自豪 2024-11-03 17:00:51

在 POSIX 平台上:

open("/dev/urandom","rb").read(4)

使用 /dev/random 获得更好的随机化效果。

On POSIX platforms:

open("/dev/urandom","rb").read(4)

Use /dev/random for better randomization.

泪意 2024-11-03 17:00:51

您想要将中间 4 个字节设置为随机值吗?

buf = '\x00' + ''.join(chr(random.randint(0,255)) for _ in range(4)) + '\x00'

Do you want the middle 4 bytes to be set to a random value?

buf = '\x00' + ''.join(chr(random.randint(0,255)) for _ in range(4)) + '\x00'
明月松间行 2024-11-03 17:00:51

这可以用来生成随机字节字符串(将 n 替换为所需的数量):

import random
random_bytes = bytes([random.randrange(0, 256) for _ in range(0, n)])
-or-
random_bytes = bytes([random.randint(0, 255) for _ in range(0, n)])
-or-
random_bytes = bytes([random.getrandbits(8) for _ in range(0, n)])

具体问题的答案将是:

import random
buf = b'\x00' + bytes([random.randrange(0, 256) for _ in range(0, 4)]) + b'\x00'
-or-
buf = b'\x00' + bytes([random.randint(0, 255) for _ in range(0, 4)]) + b'\x00'
-or-
buf = b'\x00' + bytes([random.getrandbits(8) for _ in range(0, 4)]) + b'\x00'

正如其他人指出的那样,这不应该用于加密,但对于其他一切来说,它应该完全没问题。

This can be used to generate a string of random bytes (replace n with the desired amount):

import random
random_bytes = bytes([random.randrange(0, 256) for _ in range(0, n)])
-or-
random_bytes = bytes([random.randint(0, 255) for _ in range(0, n)])
-or-
random_bytes = bytes([random.getrandbits(8) for _ in range(0, n)])

The answer to the specific question would then be:

import random
buf = b'\x00' + bytes([random.randrange(0, 256) for _ in range(0, 4)]) + b'\x00'
-or-
buf = b'\x00' + bytes([random.randint(0, 255) for _ in range(0, 4)]) + b'\x00'
-or-
buf = b'\x00' + bytes([random.getrandbits(8) for _ in range(0, 4)]) + b'\x00'

As others pointed out, this should not be used for cryptography, but for everything else it should be perfectly fine.

复古式 2024-11-03 17:00:51

我喜欢使用 numpy 库来实现这一点。

import numpy as np

X_1KB = 1024
X_256KB = 256 * X_1KB
X_1MB = 1024 * 1024
X_4MB = 4 * X_1MB
X_32MB = 32 * X_1MB
X_64MB = 2 * X_32MB
X_128MB = X_1MB * 128


np.random.bytes( X_1MB )

I like using numpy library for that.

import numpy as np

X_1KB = 1024
X_256KB = 256 * X_1KB
X_1MB = 1024 * 1024
X_4MB = 4 * X_1MB
X_32MB = 32 * X_1MB
X_64MB = 2 * X_32MB
X_128MB = X_1MB * 128


np.random.bytes( X_1MB )
独孤求败 2024-11-03 17:00:51

简单的:

import functools, random, operator
functools.reduce(operator.add, ('%c' % random.randint(0, 255) for i in range(4)))

Simple:

import functools, random, operator
functools.reduce(operator.add, ('%c' % random.randint(0, 255) for i in range(4)))
风吹雨成花 2024-11-03 17:00:51
from random import randint 
rstr = ''.join( randint(0, 255) for i in range(4) )
from random import randint 
rstr = ''.join( randint(0, 255) for i in range(4) )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文