Signature

Signature (전자 서명)

디지털 서명 알고리즘의 기능을 애플리케이션에 제공하는 클래스입니다. 디지털 서명은 디지털 데이터의 인증(authentication)과 무결성 보장(integrity assurance)에 사용됩니다.

출처: Java API Reference

본문

Signature 클래스는 디지털 서명 알고리즘의 기능을 제공합니다. 알고리즘은 언제든 SIGN 모드와 VERIFY 모드 두 가지 중 하나만 가질 수 있습니다.

  • SIGN 모드 — 서명할 데이터의 바이트를 initSign 메서드로 초기화하고, 서명용으로 모드를 구성합니다.
  • VERIFY 모드 — 검증할 데이터의 바이트를 initVerify 메서드로 초기화하고, 검증용으로 모드를 구성합니다.

알고리즘 객체는 initSign이나 initVerify로 서명·검증 과정 전체에 대해 한 번 초기화됩니다. 그 뒤 서명할 데이터는 update 메서드들의 일부에 전달됩니다. 마지막으로 서명을 생성(sign)하거나 검증(verify)합니다. 전체 과정은 reset 메서드나 동등한 initSign/initVerify 호출로 재설정할 수 있습니다. reset 메서드는 알고리즘 객체의 상태를 기본 상태(또는 initSign/initVerify가 정의한 상태) 외의 다른 상태로 설정해서는 안 됩니다.

서명 예시는 다음과 같습니다.

Signature s = Signature.getInstance("SHA256withRSA");
s.initSign(privateKey);
byte[] sig = s.sign();
Signature t = Signature.getInstance("SHA256withRSA");
t.initVerify(publicKey);
t.update(data);
boolean verifies = t.verify(sig);

Signature 객체의 상태는 sign, verify, initSign, initVerify, setParameter, reset(JDK 21 추가) 메서드로만 바뀔 수 있습니다. 성공적인 sign/verify 호출 후 reset를 호출한 뒤 다시 initSign/initVerify를 호출하면 객체를 재사용할 수 있습니다.

모든 Java 플랫폼 구현은 SHA256withRSA, SHA384withRSA, SHA512withRSA, SHA256withDSA, SHA384withDSA, SHA512withDSA, SHA256withECDSA, SHA384withECDSA, SHA512withECDSA 표준 서명 알고리즘을 지원해야 합니다.

Signature의 주요 메서드는 다음과 같습니다.

  • public static Signature getInstance(String algorithm) — 지정된 서명 알고리즘을 구현하는 Signature 객체를 반환합니다.
  • public static Signature getInstance(String algorithm, String provider), public static Signature getInstance(String algorithm, Provider provider) — 지정된 프로바이더에서 알고리즘을 지원하는 객체를 반환합니다.
  • public final Provider getProvider() — 서명 객체의 프로바이더를 반환합니다.
  • public final String getAlgorithm() — 서명 알고리즘의 이름을 반환합니다.
  • public final void initVerify(PublicKey publicKey), public final void initVerify(Certificate certificate) — 검증 모드로 초기화합니다.
  • public final void initSign(PrivateKey privateKey) — 서명 모드로 초기화합니다. 난수 소스나 AlgorithmParameterSpec을 지정하는 변형도 있습니다.
  • public final void setParameter(AlgorithmParameterSpec params) — 알고리즘 특유의 매개변수를 설정합니다.
  • public final AlgorithmParameterSpec getParameters() — 알고리즘 특유의 매개변수를 반환합니다.
  • public final void update(byte b), update(byte[] data), update(byte[] data, int off, int len), update(ByteBuffer data) — 서명·검증할 데이터를 갱신합니다.
  • public final byte[] sign() — 모든 데이터 갱신이 끝난 뒤 서명을 생성합니다.
  • public int verify(byte[] signature) — 주어진 서명을 검증합니다.
  • public final void reset() — 서명 엔진을 기본 상태로 재설정합니다.
  • public Object clone() — 복제본을 반환합니다.

지정된 서명 알고리즘을 지원하는 프로바이더가 없으면 NoSuchAlgorithmException이 발생할 수 있습니다.

더 알아보기 (Learn more)

Java 공식 API