appIdentityService.signForApp(Java)和app_identity.sign_blob(Python)结果不同

时间:2018-06-25 05:10:40

标签: google-app-engine google-cloud-platform google-identity

我需要使用Google Cloud appIdentity签署JWT。

我尝试使用python代理,它正在工作。但是Java客户端给出了签名错误。 因此,我运行测试代码以从Java和Python获取相同输入的签名。返回不同的结果。

python代码

edai.Search

java代码

import array
from google.appengine.api import app_identity

header_and_payload = "test"
(key_name, signature) = app_identity.sign_blob(header_and_payload)
print array.array('B', signature)       

Python输出

import com.google.appengine.api.appidentity.AppIdentityService;
import com.google.appengine.api.appidentity.AppIdentityServiceFactory;

headerAndPayload = "test";
AppIdentityService appIdentityService = AppIdentityServiceFactory.getAppIdentityService();
AppIdentityService.SigningResult signingResult = appIdentityService.signForApp(headerAndPayload.getBytes());

System.out.println(Arrays.toString(signingResult.getSignature()));

java输出

[205, 130, 214, 28, 19, 7, 233, 69, 92, 161, 8, 160, 36, 162, 149, 125, 5, 100, 8, 219, 244, 235, 188, 126, 118, 45, 176, 63, 61, 88, 91, 151, 151, 114, 228, 31, 85, 209, 117, 134, 66, 120, 13, 159, 10, 155, 70, 16, 110, 56, 212, 79, 165, 40, 222, 46, 26, 74, 182, 80, 223, 57, 244, 44, 224, 122, 230, 184, 114, 236, 158, 204, 145, 152, 133, 131, 115, 43, 224, 132, 219, 232, 186, 237, 82, 86, 243, 194, 155, 127, 26, 227, 19, 165, 142, 216, 238, 163, 99, 251, 41, 191, 164, 206, 85, 239, 64, 133, 41, 49, 120, 235, 120, 226, 96, 224, 105, 68, 81, 186, 184, 65, 233, 129, 211, 231, 211, 135, 15, 88, 35, 20, 217, 95, 56, 215, 134, 71, 210, 28, 43, 22, 231, 69, 134, 116, 227, 161, 202, 94, 54, 222, 132, 158, 108, 45, 73, 68, 240, 90, 59, 139, 222, 118, 6, 82, 162, 198, 143, 7, 233, 148, 233, 232, 101, 135, 182, 71, 148, 136, 246, 168, 5, 28, 94, 11, 10, 78, 147, 4, 200, 36, 79, 244, 117, 223, 114, 33, 2, 206, 13, 66, 204, 201, 102, 147, 237, 83, 83, 17, 221, 16, 136, 206, 115, 141, 32, 149, 131, 136, 183, 96, 51, 31, 212, 174, 245, 120, 18, 120, 191, 174, 90, 111, 122, 136, 96, 152, 81, 8, 72, 52, 33, 46, 227, 241, 41, 77, 40, 176, 97, 189, 195, 197, 202, 71]

Java代码有什么问题?谢谢。

1 个答案:

答案 0 :(得分:0)

在此行:

print array.array('B', signature)

您正在根据docs将选项'B'用于未签名的字符。如果您使用

中的选项'b'(用于签名字符)
print array.array('b', signature) 

对于Java和python版本,您将获得相同的结果。

我用PythonJava的示例代码进行了测试。在Python示例中,我将message变量设置为与Java中相同的值:

message = "abcdefg"

并添加以下行:

self.response.write('Signature Array: {}\n'.format(array.array('b', signature)))

希望这会有所帮助。