-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from sigstore/signer-utils
Add signer utilities
- Loading branch information
Showing
4 changed files
with
131 additions
and
27 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
src/main/java/dev/sigstore/encryption/signers/EcdsaSigner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2022 The Sigstore Authors. | ||
* | ||
* 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. | ||
*/ | ||
package dev.sigstore.encryption.signers; | ||
|
||
import java.nio.charset.Charset; | ||
import java.security.*; | ||
|
||
/** ECDSA signer, use {@link Signers#newEcdsaSigner()} to instantiate}. */ | ||
public class EcdsaSigner implements Signer { | ||
|
||
private final KeyPair keyPair; | ||
|
||
EcdsaSigner(KeyPair keyPair) { | ||
this.keyPair = keyPair; | ||
} | ||
|
||
@Override | ||
public PublicKey getPublicKey() { | ||
return keyPair.getPublic(); | ||
} | ||
|
||
@Override | ||
public byte[] sign(String content, Charset charset) | ||
throws NoSuchAlgorithmException, InvalidKeyException, SignatureException { | ||
return sign(content.getBytes(charset)); | ||
} | ||
|
||
@Override | ||
public byte[] sign(byte[] content) | ||
throws NoSuchAlgorithmException, InvalidKeyException, SignatureException { | ||
Signature signature = Signature.getInstance("SHA256withECDSA"); | ||
signature.initSign(keyPair.getPrivate()); | ||
signature.update(content); | ||
return signature.sign(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright 2022 The Sigstore Authors. | ||
* | ||
* 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. | ||
*/ | ||
package dev.sigstore.encryption.signers; | ||
|
||
import java.nio.charset.Charset; | ||
import java.security.InvalidKeyException; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.PublicKey; | ||
import java.security.SignatureException; | ||
|
||
/** A signing helper that wraps common signing operations for use within this library. */ | ||
public interface Signer { | ||
|
||
/** Return the public key associated with this signer. */ | ||
PublicKey getPublicKey(); | ||
|
||
/** | ||
* Sign the content. Implementations should use an algorithm that hashes with sha256 before | ||
* signing. | ||
*/ | ||
byte[] sign(String content, Charset charset) | ||
throws NoSuchAlgorithmException, InvalidKeyException, SignatureException; | ||
|
||
/** | ||
* Sign the content. Implementations should use an algorithm that hashes with sha256 before | ||
* signing. | ||
*/ | ||
byte[] sign(byte[] content) | ||
throws NoSuchAlgorithmException, InvalidKeyException, SignatureException; | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/dev/sigstore/encryption/signers/Signers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright 2022 The Sigstore Authors. | ||
* | ||
* 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. | ||
*/ | ||
package dev.sigstore.encryption.signers; | ||
|
||
import java.security.KeyPairGenerator; | ||
import java.security.NoSuchAlgorithmException; | ||
|
||
/** Factory class for creation of signers. */ | ||
public class Signers { | ||
|
||
/** Create a new ECDSA signer with 256 bit keysize */ | ||
public static EcdsaSigner newEcdsaSigner() throws NoSuchAlgorithmException { | ||
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC"); | ||
keyGen.initialize(256); | ||
return new EcdsaSigner(keyGen.generateKeyPair()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters