/** * Creates an entry in the primitive table. * * @return the added entry */ protected Entry<P> addPrimitive(final P primitive, Keyset.Key key) throws GeneralSecurityException { Entry<P> entry = new Entry<P>( primitive, CryptoFormat.getOutputPrefix(key), key.getStatus(), key.getOutputPrefixType()); List<Entry<P>> list = new ArrayList<Entry<P>>(); list.add(entry); // Cannot use [] as keys in hash map, convert to string. String identifier = new String(entry.getIdentifier(), UTF_8); List<Entry<P>> existing = primitives.put(identifier, Collections.unmodifiableList(list)); if (existing != null) { List<Entry<P>> newList = new ArrayList<Entry<P>>(); newList.addAll(existing); newList.add(entry); primitives.put(identifier, Collections.unmodifiableList(newList)); } return entry; } }
@Override public byte[] sign(final byte[] data) throws GeneralSecurityException { if (primitives.getPrimary().getOutputPrefixType().equals(OutputPrefixType.LEGACY)) { byte[] formatVersion = new byte[] {CryptoFormat.LEGACY_START_BYTE}; return Bytes.concat( primitives.getPrimary().getIdentifier(), primitives.getPrimary().getPrimitive().sign(Bytes.concat(data, formatVersion))); } return Bytes.concat( primitives.getPrimary().getIdentifier(), primitives.getPrimary().getPrimitive().sign(data)); } };
@Override public byte[] computeMac(final byte[] data) throws GeneralSecurityException { if (primitives.getPrimary().getOutputPrefixType().equals(OutputPrefixType.LEGACY)) { return Bytes.concat( primitives.getPrimary().getIdentifier(), primitives.getPrimary().getPrimitive().computeMac(Bytes.concat(data, formatVersion))); } return Bytes.concat( primitives.getPrimary().getIdentifier(), primitives.getPrimary().getPrimitive().computeMac(data)); }
@Override public byte[] encrypt(final byte[] plaintext, final byte[] associatedData) throws GeneralSecurityException { return Bytes.concat( pset.getPrimary().getIdentifier(), pset.getPrimary().getPrimitive().encrypt(plaintext, associatedData)); }
@Override public byte[] encryptDeterministically(final byte[] plaintext, final byte[] associatedData) throws GeneralSecurityException { return Bytes.concat( primitives.getPrimary().getIdentifier(), primitives .getPrimary() .getPrimitive() .encryptDeterministically(plaintext, associatedData)); }
@Override public byte[] encrypt(final byte[] plaintext, final byte[] contextInfo) throws GeneralSecurityException { return Bytes.concat( primitives.getPrimary().getIdentifier(), primitives.getPrimary().getPrimitive().encrypt(plaintext, contextInfo)); } };