DnsChallenge: return digest for TXT record

pull/17/merge
Richard Körber 2015-12-19 16:38:32 +01:00
parent becab450d8
commit 1e1056d692
2 changed files with 21 additions and 0 deletions

View File

@ -13,6 +13,10 @@
*/
package org.shredzone.acme4j.challenge;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.jose4j.base64url.Base64Url;
import org.shredzone.acme4j.Account;
import org.shredzone.acme4j.util.ClaimBuilder;
@ -55,6 +59,22 @@ public class DnsChallenge extends GenericChallenge {
return authorization;
}
/**
* Returns the digest string to be set in the domain's {@code _acme-challenge} TXT
* record.
*/
public String getDigest() {
try {
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(getAuthorization().getBytes("UTF-8"));
byte[] digest = md.digest();
return Base64Url.encode(digest);
} catch (NoSuchAlgorithmException | UnsupportedEncodingException ex) {
// both should be standard in JDK...
throw new RuntimeException(ex);
}
}
@Override
public void authorize(Account account) {
super.authorize(account);

View File

@ -63,6 +63,7 @@ public class DnsChallengeTest {
assertThat(challenge.getToken(), is(TOKEN));
assertThat(challenge.getAuthorization(), is(KEY_AUTHORIZATION));
assertThat(challenge.getDigest(), is("rzMmotrIgsithyBYc0vgiLUEEKYx0WetQRgEF2JIozA"));
ClaimBuilder cb = new ClaimBuilder();
challenge.marshall(cb);