[英]how to generate s3 style access/secret key using java
I am trying to generate access keys and secret keys in the same fashion as S3 using java but am having some trouble.我正在尝试使用 java 以与 S3 相同的方式生成访问密钥和密钥,但遇到了一些麻烦。
As a starting point I am looking at this bouncy castle example , I have this code up and running but am not sure of two things 1) how to set it up to use the same key generation as s3 which uses HMAC-SHA1 as outlined here and 2) how to get the friendly public/private key strings out for the the user.作为一个起点,我正在查看这个充气城堡示例,我已经启动并运行了这段代码,但我不确定两件事 1) 如何设置它以使用与使用 HMAC-SHA1 的 s3 相同的密钥生成,如此处所述2)如何为用户获取友好的公钥/私钥字符串。
You may have guessed I am new to java encryption and the bouncy castle libraries, however I did find JCEKeyGenerator.HMACSHA1 in the bc docs but am unable to find an example of its use.您可能已经猜到我是 java 加密和充气城堡库的新手,但是我确实在 bc 文档中找到了 JCEKeyGenerator.HMACSHA1,但找不到它的使用示例。 Any help would be greatly appreciated.
任何帮助将不胜感激。
Thanks.谢谢。
You'll need to make use of javax.crypto.KeyGenerator
to create the AWSAccessKeyId
and the AWSSecretAccessKey
:您需要使用
javax.crypto.KeyGenerator
来创建AWSAccessKeyId
和AWSSecretAccessKey
:
javax.crypto.KeyGenerator generator = javax.crypto.KeyGenerator.getInstance("HMACSHA1");
generator.init(120);
byte[] awsAccessKeyId = generator.generateKey().getEncoded();
generator.init(240);
byte[] awsSecretAccessKey = generator.generateKey().getEncoded();
Then, you'll want to base64 encode the bytes (this uses MimeUtility from mail.jar):然后,您需要 base64 对字节进行编码(这使用来自 mail.jar 的 MimeUtility):
final ByteArrayOutputStream encoded = new ByteArrayOutputStream();
final OutputStream encoder = javax.mail.internet.MimeUtility.encode(encoded, "base64");
encoder.write(awsAccessKeyId);
encoder.flush();
encoder.close();
String accessKeyId = new String(encoded.toByteArray(), encoding).replaceAll("[\\r\\n]", "");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.