是否可以使用用户输入在 CMAKE 中添加 post_build_step ?
我们希望通过 CMAKE 中的 post_build_step 来加密我们的二进制文件。
让我们想象一下以下 python 脚本 (enc.py)
import base64
import hashlib
from Crypto.Cipher import AES
from Crypto import Random
BLOCK_SIZE = 16
pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * chr(BLOCK_SIZE - len(s) % BLOCK_SIZE)
def encrypt(raw, password):
private_key = hashlib.sha256(password.encode("utf-8")).digest()
raw = pad(raw)
iv = Random.new().read(AES.block_size)
cipher = AES.new(private_key, AES.MODE_CBC, iv)
res = cipher.encrypt(raw.encode("utf8"))
return base64.b64encode(res)
text = "the binary to encrypt..."
passw = input("Enter encryption password: ")
encrypt_text = encrypt(text, passw)
print("origin text: {}".format(text))
print("encrypted text: {}".format(encrypt_text))
和以下 cmakelists.txt
# Check if python is available
find_package(Python3 COMPONENTS Interpreter)
if (NOT ${Python3_FOUND})
message(FATAL_ERROR "Python is needed!")
endif()
add_custom_command(TARGET APP_NAME POST_BUILD
COMMAND ${Python3_EXECUTABLE} ${SCRIPTS_DIR}/enc.py )
这是常见做法,还是有更好的方法?
考虑到,这也必须在像 Jenkins 这样的构建服务器上运行。
We want to encrypt our binaries via a post_build_step in CMAKE.
Lets imagine the following python script (enc.py)
import base64
import hashlib
from Crypto.Cipher import AES
from Crypto import Random
BLOCK_SIZE = 16
pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * chr(BLOCK_SIZE - len(s) % BLOCK_SIZE)
def encrypt(raw, password):
private_key = hashlib.sha256(password.encode("utf-8")).digest()
raw = pad(raw)
iv = Random.new().read(AES.block_size)
cipher = AES.new(private_key, AES.MODE_CBC, iv)
res = cipher.encrypt(raw.encode("utf8"))
return base64.b64encode(res)
text = "the binary to encrypt..."
passw = input("Enter encryption password: ")
encrypt_text = encrypt(text, passw)
print("origin text: {}".format(text))
print("encrypted text: {}".format(encrypt_text))
And the following cmakelists.txt
# Check if python is available
find_package(Python3 COMPONENTS Interpreter)
if (NOT ${Python3_FOUND})
message(FATAL_ERROR "Python is needed!")
endif()
add_custom_command(TARGET APP_NAME POST_BUILD
COMMAND ${Python3_EXECUTABLE} ${SCRIPTS_DIR}/enc.py )
Is this common practice, or is there a better way?
Considering, this has also to run on a build server like Jenkins.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论