Created
July 24, 2012 23:33
-
-
Save siosio/3173380 to your computer and use it in GitHub Desktop.
デジタル署名の作成&検証
This file contains hidden or 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
import java.security.KeyStore | |
import java.security.Signature | |
// KeyStoreを使用して証明書をロード | |
KeyStore keyStore = KeyStore.getInstance("PKCS12"); | |
FileInputStream stream = new FileInputStream("certificate.p12"); | |
keyStore.load(stream, "password".chars); | |
stream.close(); | |
// 秘密鍵と公開鍵を取得するためのalias | |
String alias = keyStore.aliases().find {keyStore.isKeyEntry(it)} | |
// 秘密鍵 | |
def privateKey = keyStore.getKey(alias, "password".chars) | |
// 公開鍵 | |
def publicKey = keyStore.getCertificate(alias).publicKey | |
// 秘密鍵を使ってデジタル署名の生成 | |
def signature = Signature.getInstance("SHA1withRSA") | |
signature.initSign(privateKey) | |
signature.update("しおしお".bytes) | |
def sign = signature.sign() | |
// 公開鍵を使ってデジタル署名の検証 | |
def verifier = Signature.getInstance("SHA1withRSA") | |
verifier.initVerify(publicKey) | |
verifier.update("しおしお".bytes) | |
def verify = verifier.verify(sign) | |
println "verify = ${verify}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment