Path: blob/master/test/jdk/java/security/Signature/SignWithOutputBuffer.java
41152 views
/*1* Copyright (c) 1998, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 411489626* @summary Signature should support a sign() method that places the signature27* in an already existing array.28*/2930import java.security.*;3132public class SignWithOutputBuffer {3334public static void main(String[] args) throws Exception {3536int numBytes;3738KeyPairGenerator kpGen = KeyPairGenerator.getInstance("DSA");39kpGen.initialize(512);40KeyPair kp = kpGen.genKeyPair();4142Signature sig = Signature.getInstance("DSS");43sig.initSign(kp.getPrivate());44sig.update((byte)0xff);4546// Allocate buffer for signature. According to BSAFE, the size of the47// signature may be as many as 48 bytes.48// First, let's allocate a buffer that's too short.49byte[] out = new byte[10];50try {51numBytes = sig.sign(out, 0, out.length);52} catch (SignatureException e) {53System.out.println(e);54}5556// Now repeat the same with a buffer that's big enough57sig = Signature.getInstance("DSS");58sig.initSign(kp.getPrivate());59sig.update((byte)0xff);60out = new byte[48];61numBytes = sig.sign(out, 0, out.length);6263System.out.println("Signature len="+numBytes);64}65}666768