生成签名以后的URL的示例代码如下:
创新互联公司-专业网站定制、快速模板网站建设、高性价比乐陵网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式乐陵网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖乐陵地区。费用合理售后完善,十多年实体公司更值得信赖。
// Generate a presigned URL
Date expires = new Date (new Date().getTime() 1000 * 60); // 1 minute to expire
GeneratePresignedUrlRequest generatePresignedUrlRequest =
new GeneratePresignedUrlRequest(bucketName, key);
generatePresignedUrlRequest.setExpiration(expires);
URL url = client.generatePresignedUrl(generatePresignedUrlRequest);
System.out.println(url.toString());
这段代码假定指定bucketName和key的Object已经上传到OSS,用户可以根据修改设定expires,即过期时间。更详细的操作可以参考OSSClient#generatePresignedUrl方法和GeneratePresignedUrlRequest类的帮助。
【方案1】
package ECDSA;
import com.sun.org.apache.xerces.internal.impl.dv.util.HexBin;
import java.security.*;
import java.security.interfaces.ECPrivateKey;
import java.security.interfaces.ECPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
public class Ecdsa {
private static String src = "hello berber" ;
public static void main(String []args){
jdkECDSA();
}
public static void jdkECDSA(){
// 1.初始化密钥
try{
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC");
keyPairGenerator.initialize(256);
KeyPair keyPair = keyPairGenerator.generateKeyPair() ;
ECPublicKey ecPublicKey = (ECPublicKey)keyPair.getPublic() ;
ECPrivateKey ecPrivateKey = (ECPrivateKey)keyPair.getPrivate() ;
// 执行签名
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(ecPrivateKey.getEncoded());
KeyFactory keyFactory = KeyFactory.getInstance("EC") ;
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec) ;
Signature signature = Signature.getInstance("SHA1withECDSA");
signature.initSign(privateKey);
signature.update(src.getBytes());
byte []arr = signature.sign();
System.out.println("jdk ecdsa sign :"+ HexBin.encode(arr));
// 验证签名
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(ecPublicKey.getEncoded());
keyFactory = KeyFactory.getInstance("EC");
PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
signature = Signature.getInstance("SHA1withECDSA");
signature.initVerify(publicKey);
signature.update(src.getBytes());
boolean bool = signature.verify(arr);
System.out.println("jdk ecdsa verify:"+bool);
}catch(Exception e){
}
}
}
Java数字签名——ECDSA算法
【方案2】
public class MyTest {
/**
* @param args
*/
public static void main(String[] args) {
new MyTest().getSign();
}
void getSign() {
// Get the instance of the Key Generator with "EC" algorithm
try {
KeyPairGenerator g = KeyPairGenerator.getInstance("EC");
ECGenParameterSpec kpgparams = new ECGenParameterSpec("secp256r1");
g.initialize(kpgparams);
KeyPair pair = g.generateKeyPair();
// Instance of signature class with SHA256withECDSA algorithm
Signature ecdsaSign = Signature.getInstance("SHA256withECDSA");
ecdsaSign.initSign(pair.getPrivate());
System.out.println("Private Keys is::" + pair.getPrivate());
System.out.println("Public Keys is::" + pair.getPublic());
String msg = "text ecdsa with sha256";//getSHA256(msg)
ecdsaSign.update((msg + pair.getPrivate().toString())
.getBytes("UTF-8"));
byte[] signature = ecdsaSign.sign();
System.out.println("Signature is::"
+ new BigInteger(1, signature).toString(16));
// Validation
ecdsaSign.initVerify(pair.getPublic());
ecdsaSign.update(signature);
if (ecdsaSign.verify(signature))
System.out.println("valid");
else
System.out.println("invalid!!!!");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}}
java – 使用secp256r1曲线和SHA256算法生
怎么验证生成的Ecdsa签名是正确的呢,可以看下这篇文章:RSA,ECC,Ecdsa,国密SM2的签名,验签,加密
用Spire.Doc for Java 可以添加及验证数字签名,参考代码:
import com.spire.pdf.*;
import com.spire.pdf.graphics.PdfImage;
import com.spire.pdf.graphics.PdfTrueTypeFont;
import com.spire.pdf.security.GraphicMode;
import com.spire.pdf.security.PdfCertificate;
import com.spire.pdf.security.PdfCertificationFlags;
import com.spire.pdf.security.PdfSignature;
import java.awt.*;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
public class AddCertificate {
public static void main(String[]args){
//加载PDF文档
PdfDocument doc = new PdfDocument();
doc.loadFromFile("test.pdf");
//加载pfx证书,及证书秘钥
PdfCertificate cert = new PdfCertificate("Cermia.pfx","123654yes!");
//添加数字签名到指定页面,并设置其位置和大小
PdfSignature signature = new PdfSignature(doc, doc.getPages().get(2), cert, "MySignature");
Rectangle2D rect = new Rectangle2D.Float();
rect.setFrame(new Point2D.Float((float) doc.getPages().get(0).getActualSize().getWidth() - 340, (float) doc.getPages().get(0).getActualSize().getHeight() - 230), new Dimension(280, 150));
signature.setBounds(rect);
//设置签名为图片加文本模式
signature.setGraphicMode(GraphicMode.Sign_Image_And_Sign_Detail);
//设置签名的内容
signature.setNameLabel("签字者:");
signature.setName("Mia");
signature.setContactInfoLabel("联系电话:");
signature.setContactInfo("02881705109");
signature.setDateLabel("日期:");
signature.setDate(new java.util.Date());
signature.setLocationInfoLabel("地点:");
signature.setLocationInfo("成都");
signature.setReasonLabel("原因:");
signature.setReason("文档所有者");
signature.setDistinguishedNameLabel("DN: ");
signature.setDistinguishedName(signature.getCertificate().get_IssuerName().getName());
signature.setSignImageSource(PdfImage.fromFile("sign.png"));
//设置签名的字体
signature.setSignDetailsFont(new PdfTrueTypeFont(new Font("Arial Unicode MS", Font.PLAIN, 9)));
//设置文档权限为禁止更改
signature.setDocumentPermissions(PdfCertificationFlags.Forbid_Changes);
signature.setCertificated(true);
//保存文档
doc.saveToFile("AddSignature.pdf");
doc.close();
}
}
数字签名添加效果:
数字签名效果
参考原文