001/* 002 * nimbus-jose-jwt 003 * 004 * Copyright 2012-2016, Connect2id Ltd and contributors. 005 * 006 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use 007 * this file except in compliance with the License. You may obtain a copy of the 008 * License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software distributed 013 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 014 * CONDITIONS OF ANY KIND, either express or implied. See the License for the 015 * specific language governing permissions and limitations under the License. 016 */ 017 018package com.nimbusds.jose.crypto.impl; 019 020 021import java.util.Collections; 022import java.util.LinkedHashSet; 023import java.util.Set; 024 025import com.nimbusds.jose.EncryptionMethod; 026import com.nimbusds.jose.JWEAlgorithm; 027import com.nimbusds.jose.util.StandardCharset; 028 029 030/** 031 * The base abstract class for password-based encrypters and decrypters of 032 * {@link com.nimbusds.jose.JWEObject JWE objects}. 033 * 034 * <p>Supports the following key management algorithms: 035 * 036 * <ul> 037 * <li>{@link com.nimbusds.jose.JWEAlgorithm#PBES2_HS256_A128KW} 038 * <li>{@link com.nimbusds.jose.JWEAlgorithm#PBES2_HS384_A192KW} 039 * <li>{@link com.nimbusds.jose.JWEAlgorithm#PBES2_HS512_A256KW} 040 * </ul> 041 * 042 * <p>Supports the following content encryption algorithms: 043 * 044 * <ul> 045 * <li>{@link com.nimbusds.jose.EncryptionMethod#A128CBC_HS256} 046 * <li>{@link com.nimbusds.jose.EncryptionMethod#A192CBC_HS384} 047 * <li>{@link com.nimbusds.jose.EncryptionMethod#A256CBC_HS512} 048 * <li>{@link com.nimbusds.jose.EncryptionMethod#A128GCM} 049 * <li>{@link com.nimbusds.jose.EncryptionMethod#A192GCM} 050 * <li>{@link com.nimbusds.jose.EncryptionMethod#A256GCM} 051 * <li>{@link com.nimbusds.jose.EncryptionMethod#A128CBC_HS256_DEPRECATED} 052 * <li>{@link com.nimbusds.jose.EncryptionMethod#A256CBC_HS512_DEPRECATED} 053 * <li>{@link com.nimbusds.jose.EncryptionMethod#XC20P} 054 * </ul> 055 * 056 * @author Vladimir Dzhuvinov 057 * @version 2016-07-26 058 */ 059public abstract class PasswordBasedCryptoProvider extends BaseJWEProvider { 060 061 062 /** 063 * The supported JWE algorithms by the password-based crypto provider 064 * class. 065 */ 066 public static final Set<JWEAlgorithm> SUPPORTED_ALGORITHMS; 067 068 069 /** 070 * The supported encryption methods by the password-base crypto 071 * provider class. 072 */ 073 public static final Set<EncryptionMethod> SUPPORTED_ENCRYPTION_METHODS = ContentCryptoProvider.SUPPORTED_ENCRYPTION_METHODS; 074 075 076 static { 077 Set<JWEAlgorithm> algs = new LinkedHashSet<>(); 078 algs.add(JWEAlgorithm.PBES2_HS256_A128KW); 079 algs.add(JWEAlgorithm.PBES2_HS384_A192KW); 080 algs.add(JWEAlgorithm.PBES2_HS512_A256KW); 081 SUPPORTED_ALGORITHMS = Collections.unmodifiableSet(algs); 082 } 083 084 085 /** 086 * The password. 087 */ 088 private final byte[] password; 089 090 091 /** 092 * Creates a new password-based encryption / decryption provider. 093 * 094 * @param password The password bytes. Must not be empty or 095 * {@code null}. 096 */ 097 protected PasswordBasedCryptoProvider(final byte[] password) { 098 099 super(SUPPORTED_ALGORITHMS, ContentCryptoProvider.SUPPORTED_ENCRYPTION_METHODS, null); 100 101 if (password == null || password.length == 0) { 102 throw new IllegalArgumentException("The password must not be null or empty"); 103 } 104 105 this.password = password; 106 } 107 108 109 /** 110 * Returns the password. 111 * 112 * @return The password bytes. 113 */ 114 public byte[] getPassword() { 115 116 return password; 117 } 118 119 120 /** 121 * Returns the password. 122 * 123 * @return The password as a UTF-8 encoded string. 124 */ 125 public String getPasswordString() { 126 127 return new String(password, StandardCharset.UTF_8); 128 } 129}