首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在python程序中运行铁锈函数

如何在python程序中运行铁锈函数
EN

Stack Overflow用户
提问于 2021-07-05 05:05:19
回答 1查看 763关注 0票数 1

我正在使用xorcipher应用程序对python和生锈函数进行基准测试,但是我不知道如何将锈迹函数添加到python代码中,有人能帮助我在buf中保存输出吗?

代码语言:javascript
运行
复制
use std::convert::TryInto;

/*
    Apply a simple XOR cipher using they specified `key` of size 
    `key_size`, to the `msg` char/byte array of size `msg_len`.

    Writes he ciphertext to the externally allocated buffer `buf`.
*/

#[no_mangle]
pub unsafe fn cipher(msg: *const i8, key: *const i8, buf: *mut i8, msg_len: usize, key_len: usize)
{
    let mut i: isize = 0;
    while i < msg_len.try_into().unwrap() {
        let key_len_i8: i8 = key_len.try_into().unwrap();
        *buf.offset(i) = *msg.offset(i) ^ (*key.offset(i) % key_len_i8);
        i = i + 1;
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-05 07:40:36

假设您的锈代码名为rs_cipher.rs。如果用以下命令编译它

代码语言:javascript
运行
复制
rustc --crate-type dylib rs_cipher.rs

您可以为您的系统获得一个本机动态库。(当然,这也可以用cargocrate-type = ["cdylib"]选项来完成)

下面是一个python代码,它加载这个库,查找函数并调用它(请参阅这些不同阶段的注释)。

代码语言:javascript
运行
复制
import sys
import os
import platform
import ctypes
import traceback

# choose application specific library name
lib_name='rs_cipher'

# determine system system specific library name
if platform.system().startswith('Windows'):
  sys_lib_name='%s.dll'%lib_name
elif platform.system().startswith('Darwin'):
  sys_lib_name='lib%s.dylib'%lib_name
else:
  sys_lib_name='lib%s.so'%lib_name

# try to load native library from various locations
# (current directory, python file directory, system path...)
ntv_lib=None
for d in [os.path.curdir,
          os.path.dirname(sys.modules[__name__].__file__),
          '']:
  path=os.path.join(d, sys_lib_name)
  try:
    ntv_lib=ctypes.CDLL(path)
    break
  except:
    # traceback.print_exc()
    pass
if ntv_lib is None:
  sys.stderr.write('cannot load library %s\n'%lib_name)
  sys.exit(1)

# try to find native function
fnct_name='cipher'
ntv_fnct=None
try:
  ntv_fnct=ntv_lib[fnct_name]
except:
  # traceback.print_exc()
  pass
if ntv_fnct is None:
  sys.stderr.write('cannot find function %s in library %s\n'%
                   (fnct_name, lib_name))
  sys.exit(1)

# describe native function prototype
ntv_fnct.restype=None # no return value
ntv_fnct.argtypes=[ctypes.c_void_p, # msg
                   ctypes.c_void_p, # key
                   ctypes.c_void_p, # buf
                   ctypes.c_size_t, # msg_len
                   ctypes.c_size_t] # key_len

# use native function
msg=(ctypes.c_int8*10)()
key=(ctypes.c_int8*4)()
buf=(ctypes.c_int8*len(msg))()
for i in range(len(msg)):
  msg[i]=1+10*i
for i in range(len(key)):
  key[i]=~(20*(i+2))

sys.stdout.write('~~~~ first initial state ~~~~\n')
sys.stdout.write('msg: %s\n'%[v for v in msg])
sys.stdout.write('key: %s\n'%[v for v in key])
sys.stdout.write('buf: %s\n'%[v for v in buf])
sys.stdout.write('~~~~ first call ~~~~\n')
ntv_fnct(msg, key, buf, len(msg), len(key))
sys.stdout.write('msg: %s\n'%[v for v in msg])
sys.stdout.write('key: %s\n'%[v for v in key])
sys.stdout.write('buf: %s\n'%[v for v in buf])

(msg, buf)=(buf, msg)
for i in range(len(buf)):
  buf[i]=0
sys.stdout.write('~~~~ second initial state ~~~~\n')
sys.stdout.write('msg: %s\n'%[v for v in msg])
sys.stdout.write('key: %s\n'%[v for v in key])
sys.stdout.write('buf: %s\n'%[v for v in buf])
sys.stdout.write('~~~~ second call ~~~~\n')
ntv_fnct(msg, key, buf, len(msg), len(key))
sys.stdout.write('msg: %s\n'%[v for v in msg])
sys.stdout.write('key: %s\n'%[v for v in key])
sys.stdout.write('buf: %s\n'%[v for v in buf])

运行此python代码将显示此结果。

代码语言:javascript
运行
复制
~~~~ first initial state ~~~~
msg: [1, 11, 21, 31, 41, 51, 61, 71, 81, 91]
key: [-41, -61, -81, -101]
buf: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
~~~~ first call ~~~~
msg: [1, 11, 21, 31, 41, 51, 61, 71, 81, 91]
key: [-41, -61, -81, -101]
buf: [-42, -56, -70, -124, -2, -16, -110, -36, -122, -104]
~~~~ second initial state ~~~~
msg: [-42, -56, -70, -124, -2, -16, -110, -36, -122, -104]
key: [-41, -61, -81, -101]
buf: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
~~~~ second call ~~~~
msg: [-42, -56, -70, -124, -2, -16, -110, -36, -122, -104]
key: [-41, -61, -81, -101]
buf: [1, 11, 21, 31, 41, 51, 61, 71, 81, 91]

请注意,我在您的生锈代码中替换了这两行代码。

代码语言:javascript
运行
复制
let key_len_i8: i8 = key_len.try_into().unwrap();
*buf.offset(i) = *msg.offset(i) ^ (*key.offset(i) % key_len_i8);

由这些

代码语言:javascript
运行
复制
let key_len_isize: isize = key_len.try_into().unwrap();
*buf.offset(i) = *msg.offset(i) ^ (*key.offset(i % key_len_isize));

因为key上的偏移量超出了界限。

也许,与其处理指针算法和无符号/签名转换,不如从原始指针构建通常的切片并以安全的方式使用它们吗?

代码语言:javascript
运行
复制
#[no_mangle]
pub fn cipher(
    msg_ptr: *const i8,
    key_ptr: *const i8,
    buf_ptr: *mut i8,
    msg_len: usize,
    key_len: usize,
) {
    let msg = unsafe { std::slice::from_raw_parts(msg_ptr, msg_len) };
    let key = unsafe { std::slice::from_raw_parts(key_ptr, key_len) };
    let buf = unsafe { std::slice::from_raw_parts_mut(buf_ptr, msg_len) };
    for i in 0..msg_len {
        buf[i] = msg[i] ^ key[i % key_len];
    }
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68250924

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档