Package ch.threema.apitool
Class CryptTool
java.lang.Object
ch.threema.apitool.CryptTool
Contains static methods to do various Threema cryptography related tasks.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]decrypt(byte[] box, byte[] privateKey, byte[] publicKey, byte[] nonce) Decrypt an NaCl box using the recipient's private key and the sender's public key.static byte[]decryptFileData(byte[] fileData, byte[] secret) Decrypt symmetrically encrypted file data.static byte[]decryptFileThumbnailData(byte[] fileData, byte[] secret) Decrypt symmetrically encrypted file thumbnail data.static ThreemaMessagedecryptMessage(byte[] box, byte[] recipientPrivateKey, byte[] senderPublicKey, byte[] nonce) Decrypt a message.static byte[]derivePublicKey(byte[] privateKey) Return the public key that corresponds with a given private key.static EncryptResultencrypt(byte[] data, byte[] privateKey, byte[] publicKey) Encrypt data using NaCl asymmetric ("box") encryption.static EncryptResultencryptBallotCreateMessage(byte[] ballotId, String description, State state, VotingMode votingMode, ResultsDisclosureType resultsDisclosureType, int order, DisplayMode displayMode, List<BallotChoice> choices, List<String> participants, byte[] senderPrivateKey, byte[] recipientPublicKey) Encrypt a Ballotstatic EncryptResultencryptBallotVoteMessage(byte[] creator, byte[] ballotId, List<VoteChoice> votes, byte[] senderPrivateKey, byte[] recipientPublicKey) Encrypt a Ballotstatic EncryptResultencryptDeliveryReceipt(DeliveryReceipt.Type receiptType, List<MessageId> ackedMessageIds, byte[] senderPrivateKey, byte[] recipientPublicKey) Encrypt a Deliverystatic EncryptResultencryptFileData(byte[] data) Encrypt file data using NaCl symmetric encryption with a random key.static EncryptResultencryptFileMessage(byte[] blobId, byte[] thumbnailBlobId, String thumbnailMediaType, byte[] encryptionKey, String mimeType, String fileName, int size, String caption, FileRenderingType renderingType, String correlationId, Map<String, Object> metadata, byte[] senderPrivateKey, byte[] recipientPublicKey) Encrypt a Filestatic EncryptResultencryptFileThumbnailData(byte[] data, byte[] encryptionKey) Encrypt file thumbnail data using NaCl symmetric encryption with a random key.static EncryptResultencryptGroupBallotCreateMessage(GroupId groupId, byte[] ballotId, String description, State state, VotingMode votingMode, ResultsDisclosureType resultsDisclosureType, int order, DisplayMode displayMode, List<BallotChoice> choices, List<String> participants, byte[] senderPrivateKey, byte[] recipientPublicKey) Encrypt a Groupstatic EncryptResultencryptGroupBallotVoteMessage(GroupId groupId, byte[] creator, byte[] ballotId, List<VoteChoice> votes, byte[] senderPrivateKey, byte[] recipientPublicKey) Encrypt a Groupstatic EncryptResultencryptGroupCreateMessage(GroupId groupId, List<String> members, byte[] senderPrivateKey, byte[] recipientPublicKey) Encrypt a Groupstatic EncryptResultencryptGroupDeletePhoto(GroupId groupId, byte[] senderPrivateKey, byte[] recipientPublicKey) Encrypt a Groupstatic EncryptResultencryptGroupDeliveryReceipt(GroupId groupId, DeliveryReceipt.Type receiptType, List<MessageId> ackedMessageIds, byte[] senderPrivateKey, byte[] recipientPublicKey) Encrypt a Groupstatic EncryptResultencryptGroupFileMessage(GroupId groupId, byte[] blobId, byte[] thumbnailBlobId, String thumbnailMediaType, byte[] encryptionKey, String mimeType, String fileName, int size, String caption, FileRenderingType renderingType, String correlationId, Map<String, Object> metadata, byte[] senderPrivateKey, byte[] recipientPublicKey) Encrypt a Groupstatic EncryptResultencryptGroupLeaveMessage(GroupId groupId, byte[] senderPrivateKey, byte[] recipientPublicKey) Encrypt a Groupstatic EncryptResultencryptGroupLocationMessage(GroupId groupId, String latitude, String longitude, Float accuracy, String poiName, String address, byte[] senderPrivateKey, byte[] recipientPublicKey) Encrypt a Groupstatic EncryptResultencryptGroupRenameMessage(GroupId groupId, String groupName, byte[] senderPrivateKey, byte[] recipientPublicKey) Encrypt a Groupstatic EncryptResultencryptGroupRequestSyncMessage(GroupId groupId, byte[] senderPrivateKey, byte[] recipientPublicKey) Encrypt a Groupstatic EncryptResultencryptGroupSetPhoto(GroupId groupId, byte[] blobId, int size, byte[] encryptionKey, byte[] senderPrivateKey, byte[] recipientPublicKey) Encrypt a Groupstatic EncryptResultencryptGroupTextMessage(GroupId groupId, String text, byte[] senderPrivateKey, byte[] recipientPublicKey) Encrypt a Groupstatic EncryptResultencryptImageMessage(EncryptResult encryptResult, UploadResult uploadResult, byte[] senderPrivateKey, byte[] recipientPublicKey) Deprecated.static EncryptResultencryptLocationMessage(String latitude, String longitude, Float accuracy, String poiName, String address, byte[] senderPrivateKey, byte[] recipientPublicKey) Encrypt a Locationstatic EncryptResultencryptTextMessage(String text, byte[] senderPrivateKey, byte[] recipientPublicKey) Encrypt a Textstatic voidgenerateKeyPair(byte[] privateKey, byte[] publicKey) Generate a new key pair.static byte[]Hashes an email address for identity lookup.static byte[]hashPhoneNo(String phoneNo) Hashes a phone number for identity lookup.static byte[]Generate a random nonce.
-
Constructor Details
-
CryptTool
public CryptTool()
-
-
Method Details
-
encryptTextMessage
public static EncryptResult encryptTextMessage(String text, byte[] senderPrivateKey, byte[] recipientPublicKey) Encrypt a Text- Parameters:
text- The message textsenderPrivateKey- Private key of senderrecipientPublicKey- Public key of recipient- Returns:
- Result of the Text encryption
-
encryptLocationMessage
public static EncryptResult encryptLocationMessage(String latitude, String longitude, Float accuracy, String poiName, String address, byte[] senderPrivateKey, byte[] recipientPublicKey) Encrypt a Location- Parameters:
latitude- The geographic latitudelongitude- The geographic longitudeaccuracy- The location accuracypoiName- The location nameaddress- The location addresssenderPrivateKey- Private key of senderrecipientPublicKey- Public key of recipient- Returns:
- Result of the Location encryption
-
encryptFileMessage
public static EncryptResult encryptFileMessage(byte[] blobId, byte[] thumbnailBlobId, String thumbnailMediaType, byte[] encryptionKey, String mimeType, String fileName, int size, String caption, FileRenderingType renderingType, String correlationId, Map<String, Object> metadata, byte[] senderPrivateKey, byte[] recipientPublicKey) Encrypt a File- Parameters:
blobId- The blob IDthumbnailBlobId- The thumbnail blob IDthumbnailMediaType- The thumbnail media typeencryptionKey- The encryption keymimeType- The mime typefileName- The filenamesize- The file sizecaption- The file captionrenderingType- The rendering typecorrelationId- The correlation identifiermetadata- The metadatasenderPrivateKey- Private key of senderrecipientPublicKey- Public key of recipient- Returns:
- Result of the File encryption
-
encryptBallotCreateMessage
public static EncryptResult encryptBallotCreateMessage(byte[] ballotId, String description, State state, VotingMode votingMode, ResultsDisclosureType resultsDisclosureType, int order, DisplayMode displayMode, List<BallotChoice> choices, List<String> participants, byte[] senderPrivateKey, byte[] recipientPublicKey) Encrypt a Ballot- Parameters:
ballotId- The poll ballot identifierdescription- The group poll descriptionstate- The group poll statevotingMode- The voting moderesultsDisclosureType- The poll results disclosure typeorder- The poll results order (deprecated)displayMode- The display modechoices- The available vote choicesparticipants- The poll participantssenderPrivateKey- Private key of senderrecipientPublicKey- Public key of recipient- Returns:
- Result of the Ballot encryption
-
encryptBallotVoteMessage
public static EncryptResult encryptBallotVoteMessage(byte[] creator, byte[] ballotId, List<VoteChoice> votes, byte[] senderPrivateKey, byte[] recipientPublicKey) Encrypt a Ballot- Parameters:
creator- The group poll creatorballotId- The poll ballot identifiervotes- The votes tuplesenderPrivateKey- Private key of senderrecipientPublicKey- Public key of recipient- Returns:
- Result of the Ballot encryption
-
encryptDeliveryReceipt
public static EncryptResult encryptDeliveryReceipt(DeliveryReceipt.Type receiptType, List<MessageId> ackedMessageIds, byte[] senderPrivateKey, byte[] recipientPublicKey) Encrypt a Delivery- Parameters:
receiptType- The message receipt typeackedMessageIds- The acked message idssenderPrivateKey- Private key of senderrecipientPublicKey- Public key of recipient- Returns:
- Result of the Delivery encryption
-
encryptGroupDeliveryReceipt
public static EncryptResult encryptGroupDeliveryReceipt(GroupId groupId, DeliveryReceipt.Type receiptType, List<MessageId> ackedMessageIds, byte[] senderPrivateKey, byte[] recipientPublicKey) Encrypt a Group- Parameters:
groupId- The group identifierreceiptType- The message receipt typeackedMessageIds- The acked message idssenderPrivateKey- Private key of senderrecipientPublicKey- Public key of recipient- Returns:
- Result of the Group encryption
-
encryptGroupCreateMessage
public static EncryptResult encryptGroupCreateMessage(GroupId groupId, List<String> members, byte[] senderPrivateKey, byte[] recipientPublicKey) Encrypt a Group- Parameters:
groupId- The group identifiermembers- The group memberssenderPrivateKey- Private key of senderrecipientPublicKey- Public key of recipient- Returns:
- Result of the Group encryption
-
encryptGroupRenameMessage
public static EncryptResult encryptGroupRenameMessage(GroupId groupId, String groupName, byte[] senderPrivateKey, byte[] recipientPublicKey) Encrypt a Group- Parameters:
groupId- The group identifiergroupName- The group namesenderPrivateKey- Private key of senderrecipientPublicKey- Public key of recipient- Returns:
- Result of the Group encryption
-
encryptGroupLeaveMessage
public static EncryptResult encryptGroupLeaveMessage(GroupId groupId, byte[] senderPrivateKey, byte[] recipientPublicKey) Encrypt a Group- Parameters:
groupId- The group identifiersenderPrivateKey- Private key of senderrecipientPublicKey- Public key of recipient- Returns:
- Result of the Group encryption
-
encryptGroupSetPhoto
public static EncryptResult encryptGroupSetPhoto(GroupId groupId, byte[] blobId, int size, byte[] encryptionKey, byte[] senderPrivateKey, byte[] recipientPublicKey) Encrypt a Group- Parameters:
groupId- The group identifierblobId- The blob IDsize- The file sizeencryptionKey- The encryption keysenderPrivateKey- Private key of senderrecipientPublicKey- Public key of recipient- Returns:
- Result of the Group encryption
-
encryptGroupDeletePhoto
public static EncryptResult encryptGroupDeletePhoto(GroupId groupId, byte[] senderPrivateKey, byte[] recipientPublicKey) Encrypt a Group- Parameters:
groupId- The group identifiersenderPrivateKey- Private key of senderrecipientPublicKey- Public key of recipient- Returns:
- Result of the Group encryption
-
encryptGroupRequestSyncMessage
public static EncryptResult encryptGroupRequestSyncMessage(GroupId groupId, byte[] senderPrivateKey, byte[] recipientPublicKey) Encrypt a Group- Parameters:
groupId- The group identifiersenderPrivateKey- Private key of senderrecipientPublicKey- Public key of recipient- Returns:
- Result of the Group encryption
-
encryptGroupTextMessage
public static EncryptResult encryptGroupTextMessage(GroupId groupId, String text, byte[] senderPrivateKey, byte[] recipientPublicKey) Encrypt a Group- Parameters:
groupId- The group identifiertext- The message textsenderPrivateKey- Private key of senderrecipientPublicKey- Public key of recipient- Returns:
- Result of the Group encryption
-
encryptGroupLocationMessage
public static EncryptResult encryptGroupLocationMessage(GroupId groupId, String latitude, String longitude, Float accuracy, String poiName, String address, byte[] senderPrivateKey, byte[] recipientPublicKey) Encrypt a Group- Parameters:
groupId- The group identifierlatitude- The geographic latitudelongitude- The geographic longitudeaccuracy- The location accuracypoiName- The location nameaddress- The location addresssenderPrivateKey- Private key of senderrecipientPublicKey- Public key of recipient- Returns:
- Result of the Group encryption
-
encryptGroupFileMessage
public static EncryptResult encryptGroupFileMessage(GroupId groupId, byte[] blobId, byte[] thumbnailBlobId, String thumbnailMediaType, byte[] encryptionKey, String mimeType, String fileName, int size, String caption, FileRenderingType renderingType, String correlationId, Map<String, Object> metadata, byte[] senderPrivateKey, byte[] recipientPublicKey) Encrypt a Group- Parameters:
groupId- The group identifierblobId- The blob IDthumbnailBlobId- The thumbnail blob IDthumbnailMediaType- The thumbnail media typeencryptionKey- The encryption keymimeType- The mime typefileName- The filenamesize- The file sizecaption- The file captionrenderingType- The rendering typecorrelationId- The correlation identifiermetadata- The metadatasenderPrivateKey- Private key of senderrecipientPublicKey- Public key of recipient- Returns:
- Result of the Group encryption
-
encryptGroupBallotCreateMessage
public static EncryptResult encryptGroupBallotCreateMessage(GroupId groupId, byte[] ballotId, String description, State state, VotingMode votingMode, ResultsDisclosureType resultsDisclosureType, int order, DisplayMode displayMode, List<BallotChoice> choices, List<String> participants, byte[] senderPrivateKey, byte[] recipientPublicKey) Encrypt a Group- Parameters:
groupId- The group identifierballotId- The poll ballot identifierdescription- The group poll descriptionstate- The group poll statevotingMode- The voting moderesultsDisclosureType- The poll results disclosure typeorder- The poll results order (deprecated)displayMode- The display modechoices- The available vote choicesparticipants- The poll participantssenderPrivateKey- Private key of senderrecipientPublicKey- Public key of recipient- Returns:
- Result of the Group encryption
-
encryptGroupBallotVoteMessage
public static EncryptResult encryptGroupBallotVoteMessage(GroupId groupId, byte[] creator, byte[] ballotId, List<VoteChoice> votes, byte[] senderPrivateKey, byte[] recipientPublicKey) Encrypt a Group- Parameters:
groupId- The group identifiercreator- The group poll creatorballotId- The poll ballot identifiervotes- The votes arraysenderPrivateKey- Private key of senderrecipientPublicKey- Public key of recipient- Returns:
- Result of the Group encryption
-
encryptImageMessage
@Deprecated public static EncryptResult encryptImageMessage(EncryptResult encryptResult, UploadResult uploadResult, byte[] senderPrivateKey, byte[] recipientPublicKey) Deprecated.Encrypt an image message.- Parameters:
encryptResult- result of the image encryptionuploadResult- result of the uploadsenderPrivateKey- the private key of the sending IDrecipientPublicKey- the public key of the receiving ID- Returns:
- encrypted result
-
decrypt
public static byte[] decrypt(byte[] box, byte[] privateKey, byte[] publicKey, byte[] nonce) Decrypt an NaCl box using the recipient's private key and the sender's public key.- Parameters:
box- The box to be decryptedprivateKey- The private key of the recipientpublicKey- The public key of the sendernonce- The nonce that was used for encryption- Returns:
- The decrypted data, or null if decryption failed
-
decryptFileData
public static byte[] decryptFileData(byte[] fileData, byte[] secret) Decrypt symmetrically encrypted file data.- Parameters:
fileData- The encrypted file datasecret- The symmetric key that was used for encryption- Returns:
- The decrypted file data, or null if decryption failed
-
decryptFileThumbnailData
public static byte[] decryptFileThumbnailData(byte[] fileData, byte[] secret) Decrypt symmetrically encrypted file thumbnail data.- Parameters:
fileData- The encrypted thumbnail datasecret- The symmetric key that was used for encryption- Returns:
- The decrypted thumbnail data, or null if decryption failed
-
decryptMessage
public static ThreemaMessage decryptMessage(byte[] box, byte[] recipientPrivateKey, byte[] senderPublicKey, byte[] nonce) throws MessageParseException Decrypt a message.- Parameters:
box- the box to be decryptedrecipientPrivateKey- the private key of the receiving IDsenderPublicKey- the public key of the sending IDnonce- the nonce that was used for the encryption- Returns:
- decrypted message (text or delivery receipt)
- Throws:
MessageParseException
-
generateKeyPair
public static void generateKeyPair(byte[] privateKey, byte[] publicKey) Generate a new key pair.- Parameters:
privateKey- is used to return the generated private key (length must be NaCl.PRIVATEKEYBYTES)publicKey- is used to return the generated public key (length must be NaCl.PUBLICKEYBYTES)
-
encrypt
Encrypt data using NaCl asymmetric ("box") encryption.- Parameters:
data- the data to be encryptedprivateKey- is used to return the generated private key (length must be NaCl.PRIVATEKEYBYTES)publicKey- is used to return the generated public key (length must be NaCl.PUBLICKEYBYTES)
-
encryptFileData
Encrypt file data using NaCl symmetric encryption with a random key.- Parameters:
data- the file contents to be encrypted- Returns:
- the encryption result including the random key
-
encryptFileThumbnailData
Encrypt file thumbnail data using NaCl symmetric encryption with a random key.- Parameters:
data- the file contents to be encrypted- Returns:
- the encryption result including the random key
-
hashEmail
Hashes an email address for identity lookup.- Parameters:
email- the email address- Returns:
- the raw hash
-
hashPhoneNo
Hashes a phone number for identity lookup.- Parameters:
phoneNo- the phone number- Returns:
- the raw hash
-
randomNonce
public static byte[] randomNonce()Generate a random nonce.- Returns:
- random nonce
-
derivePublicKey
public static byte[] derivePublicKey(byte[] privateKey) Return the public key that corresponds with a given private key.- Parameters:
privateKey- The private key whose public key should be derived- Returns:
- The corresponding public key.
-