Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
3a1cfa9d
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
3a1cfa9d
编写于
5月 28, 2020
作者:
Y
yangqingyou
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add crypto api for python, test=develop
上级
6ebf5b97
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
229 addition
and
1 deletion
+229
-1
CMakeLists.txt
CMakeLists.txt
+1
-1
cmake/configure.cmake
cmake/configure.cmake
+4
-0
paddle/fluid/pybind/CMakeLists.txt
paddle/fluid/pybind/CMakeLists.txt
+5
-0
paddle/fluid/pybind/crypto.cc
paddle/fluid/pybind/crypto.cc
+136
-0
paddle/fluid/pybind/crypto.h
paddle/fluid/pybind/crypto.h
+23
-0
paddle/fluid/pybind/pybind.cc
paddle/fluid/pybind/pybind.cc
+7
-0
python/paddle/fluid/tests/unittests/CMakeLists.txt
python/paddle/fluid/tests/unittests/CMakeLists.txt
+4
-0
python/paddle/fluid/tests/unittests/test_crypto.py
python/paddle/fluid/tests/unittests/test_crypto.py
+49
-0
未找到文件。
CMakeLists.txt
浏览文件 @
3a1cfa9d
...
...
@@ -88,7 +88,7 @@ option(WITH_DGC "Use DGC(Deep Gradient Compression) or not" ${WITH_DISTRIBUTE}
option
(
SANITIZER_TYPE
"Choose the type of sanitizer, options are: Address, Leak, Memory, Thread, Undefined"
OFF
)
option
(
WITH_LITE
"Compile Paddle Fluid with Lite Engine"
OFF
)
option
(
WITH_NCCL
"Compile PaddlePaddle with NCCL support"
ON
)
option
(
WITH_CRYPTO
"Compile PaddlePaddle with
paddle_crypto lib
"
ON
)
option
(
WITH_CRYPTO
"Compile PaddlePaddle with
crypto support
"
ON
)
# PY_VERSION
if
(
NOT PY_VERSION
)
...
...
cmake/configure.cmake
浏览文件 @
3a1cfa9d
...
...
@@ -154,3 +154,7 @@ endif(WITH_BRPC_RDMA)
if
(
ON_INFER
)
add_definitions
(
-DPADDLE_ON_INFERENCE
)
endif
(
ON_INFER
)
if
(
WITH_CRYPTO
)
add_definitions
(
-DPADDLE_WITH_CRYPTO
)
endif
(
WITH_CRYPTO
)
\ No newline at end of file
paddle/fluid/pybind/CMakeLists.txt
浏览文件 @
3a1cfa9d
...
...
@@ -38,6 +38,11 @@ set(PYBIND_SRCS
ir.cc
inference_api.cc
)
if
(
WITH_CRYPTO
)
set
(
PYBIND_DEPS
${
PYBIND_DEPS
}
paddle_crypto
)
set
(
PYBIND_SRCS
${
PYBIND_SRCS
}
crypto.cc
)
endif
(
WITH_CRYPTO
)
if
(
WITH_DISTRIBUTE
)
list
(
APPEND PYBIND_SRCS communicator_py.cc
)
endif
()
...
...
paddle/fluid/pybind/crypto.cc
0 → 100644
浏览文件 @
3a1cfa9d
// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "paddle/fluid/pybind/crypto.h"
#include <memory>
#include <string>
#include "paddle/fluid/framework/io/crypto/aes_cipher.h"
#include "paddle/fluid/framework/io/crypto/cipher.h"
#include "paddle/fluid/framework/io/crypto/cipher_utils.h"
namespace
py
=
pybind11
;
namespace
paddle
{
namespace
pybind
{
using
paddle
::
framework
::
AESCipher
;
using
paddle
::
framework
::
Cipher
;
using
paddle
::
framework
::
CipherFactory
;
using
paddle
::
framework
::
CipherUtils
;
namespace
{
class
PyCipher
:
public
Cipher
{
public:
using
Cipher
::
Cipher
;
// encrypt string
std
::
string
Encrypt
(
const
std
::
string
&
plaintext
,
const
std
::
string
&
key
)
override
{
PYBIND11_OVERLOAD_PURE_NAME
(
std
::
string
,
Cipher
,
"encrypt"
,
Encrypt
,
plaintext
,
key
);
}
// decrypt string
std
::
string
Decrypt
(
const
std
::
string
&
ciphertext
,
const
std
::
string
&
key
)
override
{
PYBIND11_OVERLOAD_PURE_NAME
(
std
::
string
,
Cipher
,
"decrypt"
,
Decrypt
,
ciphertext
,
key
);
}
// encrypt strings and read them to file,
void
EncryptToFile
(
const
std
::
string
&
plaintext
,
const
std
::
string
&
key
,
const
std
::
string
&
filename
)
override
{
PYBIND11_OVERLOAD_PURE_NAME
(
void
,
Cipher
,
"encrypt_to_file"
,
EncryptToFile
,
plaintext
,
key
,
filename
);
}
// read from file and decrypt them
std
::
string
DecryptFromFile
(
const
std
::
string
&
key
,
const
std
::
string
&
filename
)
override
{
PYBIND11_OVERLOAD_PURE_NAME
(
std
::
string
,
Cipher
,
"decrypt_from_file"
,
DecryptFromFile
,
key
,
filename
);
}
};
void
BindCipher
(
py
::
module
*
m
)
{
py
::
class_
<
Cipher
,
PyCipher
,
std
::
shared_ptr
<
Cipher
>>
(
*
m
,
"Cipher"
)
.
def
(
py
::
init
<>
())
.
def
(
"encrypt"
,
[](
Cipher
&
c
,
const
std
::
string
&
plaintext
,
const
std
::
string
&
key
)
{
std
::
string
ret
=
c
.
Encrypt
(
plaintext
,
key
);
return
py
::
bytes
(
ret
);
})
.
def
(
"decrypt"
,
[](
Cipher
&
c
,
const
std
::
string
&
ciphertext
,
const
std
::
string
&
key
)
{
std
::
string
ret
=
c
.
Decrypt
(
ciphertext
,
key
);
return
py
::
bytes
(
ret
);
})
.
def
(
"encrypt_to_file"
,
[](
Cipher
&
c
,
const
std
::
string
&
plaintext
,
const
std
::
string
&
key
,
const
std
::
string
&
filename
)
{
c
.
EncryptToFile
(
plaintext
,
key
,
filename
);
})
.
def
(
"decrypt_from_file"
,
[](
Cipher
&
c
,
const
std
::
string
&
key
,
const
std
::
string
&
filename
)
{
std
::
string
ret
=
c
.
DecryptFromFile
(
key
,
filename
);
return
py
::
bytes
(
ret
);
});
}
void
BindAESCipher
(
py
::
module
*
m
)
{
py
::
class_
<
AESCipher
,
Cipher
,
std
::
shared_ptr
<
AESCipher
>>
(
*
m
,
"AESCipher"
)
.
def
(
py
::
init
<>
());
}
void
BindCipherFactory
(
py
::
module
*
m
)
{
py
::
class_
<
CipherFactory
>
(
*
m
,
"CipherFactory"
)
.
def
(
py
::
init
<>
())
.
def_static
(
"create_cipher"
,
[](
const
std
::
string
&
config_file
)
{
return
CipherFactory
::
CreateCipher
(
config_file
);
},
py
::
arg
(
"config_file"
)
=
std
::
string
());
}
void
BindCipherUtils
(
py
::
module
*
m
)
{
py
::
class_
<
CipherUtils
>
(
*
m
,
"CipherUtils"
)
.
def_static
(
"gen_key"
,
[](
int
length
)
{
std
::
string
ret
=
CipherUtils
::
GenKey
(
length
);
return
py
::
bytes
(
ret
);
})
.
def_static
(
"gen_key_to_file"
,
[](
int
length
,
const
std
::
string
&
filename
)
{
std
::
string
ret
=
CipherUtils
::
GenKeyToFile
(
length
,
filename
);
return
py
::
bytes
(
ret
);
})
.
def_static
(
"read_key_from_file"
,
[](
const
std
::
string
&
filename
)
{
std
::
string
ret
=
CipherUtils
::
ReadKeyFromFile
(
filename
);
return
py
::
bytes
(
ret
);
});
}
}
// namespace
void
BindCrypto
(
py
::
module
*
m
)
{
BindCipher
(
m
);
BindCipherFactory
(
m
);
BindCipherUtils
(
m
);
BindAESCipher
(
m
);
}
}
// namespace pybind
}
// namespace paddle
paddle/fluid/pybind/crypto.h
0 → 100644
浏览文件 @
3a1cfa9d
// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include <pybind11/pybind11.h>
namespace
paddle
{
namespace
pybind
{
void
BindCrypto
(
pybind11
::
module
*
m
);
}
// namespace pybind
}
// namespace paddle
paddle/fluid/pybind/pybind.cc
浏览文件 @
3a1cfa9d
...
...
@@ -91,6 +91,10 @@ limitations under the License. */
#include "paddle/fluid/pybind/communicator_py.h"
#endif
#ifdef PADDLE_WITH_CRYPTO
#include "paddle/fluid/pybind/crypto.h"
#endif
#include "pybind11/stl.h"
DECLARE_bool
(
use_mkldnn
);
...
...
@@ -2420,6 +2424,9 @@ All parameter, weight, gradient are variables in Paddle.
BindNode
(
&
m
);
BindInferenceApi
(
&
m
);
BindDataset
(
&
m
);
#ifdef PADDLE_WITH_CRYPTO
BindCrypto
(
&
m
);
#endif
#ifdef PADDLE_WITH_DISTRIBUTE
BindCommunicator
(
&
m
);
#endif
...
...
python/paddle/fluid/tests/unittests/CMakeLists.txt
浏览文件 @
3a1cfa9d
...
...
@@ -101,6 +101,10 @@ if(WITH_GPU OR NOT WITH_MKLML)
LIST
(
REMOVE_ITEM TEST_OPS test_matmul_op_with_head
)
endif
()
if
(
NOT WITH_CRYPTO
)
LIST
(
REMOVE_ITEM TEST_OPS test_crypto
)
endif
()
function
(
py_test_modules TARGET_NAME
)
if
(
WITH_TESTING
)
set
(
options SERIAL
)
...
...
python/paddle/fluid/tests/unittests/test_crypto.py
0 → 100644
浏览文件 @
3a1cfa9d
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from
paddle.fluid.core
import
CipherUtils
from
paddle.fluid.core
import
CipherFactory
from
paddle.fluid.core
import
Cipher
import
unittest
class
CipherUtilsTestCase
(
unittest
.
TestCase
):
def
test_gen_key
(
self
):
key1
=
CipherUtils
.
gen_key
(
256
)
key2
=
CipherUtils
.
gen_key_to_file
(
256
,
"/tmp/paddle_aes_test.keyfile"
)
self
.
assertNotEquals
(
key1
,
key2
)
key3
=
CipherUtils
.
read_key_from_file
(
"/tmp/paddle_aes_test.keyfile"
)
self
.
assertEqual
(
key2
,
key3
)
self
.
assertEqual
(
len
(
key1
),
32
)
self
.
assertEqual
(
len
(
key2
),
32
)
class
CipherTestCase
(
unittest
.
TestCase
):
def
test_aes_cipher
(
self
):
plaintext
=
"hello world"
key
=
CipherUtils
.
gen_key
(
256
)
cipher
=
CipherFactory
.
create_cipher
()
ciphertext
=
cipher
.
encrypt
(
plaintext
,
key
)
cipher
.
encrypt_to_file
(
plaintext
,
key
,
"paddle_aes_test.ciphertext"
)
plaintext1
=
cipher
.
decrypt
(
ciphertext
,
key
)
plaintext2
=
cipher
.
decrypt_from_file
(
key
,
"paddle_aes_test.ciphertext"
)
self
.
assertEqual
(
plaintext
,
plaintext1
)
self
.
assertEqual
(
plaintext1
,
plaintext2
)
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录