Class CryptTool

java.lang.Object
ch.threema.apitool.CryptTool

public class CryptTool extends Object
Contains static methods to do various Threema cryptography related tasks.
  • 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 text
      senderPrivateKey - Private key of sender
      recipientPublicKey - 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 latitude
      longitude - The geographic longitude
      accuracy - The location accuracy
      poiName - The location name
      address - The location address
      senderPrivateKey - Private key of sender
      recipientPublicKey - 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 ID
      thumbnailBlobId - The thumbnail blob ID
      thumbnailMediaType - The thumbnail media type
      encryptionKey - The encryption key
      mimeType - The mime type
      fileName - The filename
      size - The file size
      caption - The file caption
      renderingType - The rendering type
      correlationId - The correlation identifier
      metadata - The metadata
      senderPrivateKey - Private key of sender
      recipientPublicKey - 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 identifier
      description - The group poll description
      state - The group poll state
      votingMode - The voting mode
      resultsDisclosureType - The poll results disclosure type
      order - The poll results order (deprecated)
      displayMode - The display mode
      choices - The available vote choices
      participants - The poll participants
      senderPrivateKey - Private key of sender
      recipientPublicKey - 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 creator
      ballotId - The poll ballot identifier
      votes - The votes tuple
      senderPrivateKey - Private key of sender
      recipientPublicKey - 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 type
      ackedMessageIds - The acked message ids
      senderPrivateKey - Private key of sender
      recipientPublicKey - 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 identifier
      receiptType - The message receipt type
      ackedMessageIds - The acked message ids
      senderPrivateKey - Private key of sender
      recipientPublicKey - 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 identifier
      members - The group members
      senderPrivateKey - Private key of sender
      recipientPublicKey - 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 identifier
      groupName - The group name
      senderPrivateKey - Private key of sender
      recipientPublicKey - 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 identifier
      senderPrivateKey - Private key of sender
      recipientPublicKey - 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 identifier
      blobId - The blob ID
      size - The file size
      encryptionKey - The encryption key
      senderPrivateKey - Private key of sender
      recipientPublicKey - 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 identifier
      senderPrivateKey - Private key of sender
      recipientPublicKey - 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 identifier
      senderPrivateKey - Private key of sender
      recipientPublicKey - 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 identifier
      text - The message text
      senderPrivateKey - Private key of sender
      recipientPublicKey - 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 identifier
      latitude - The geographic latitude
      longitude - The geographic longitude
      accuracy - The location accuracy
      poiName - The location name
      address - The location address
      senderPrivateKey - Private key of sender
      recipientPublicKey - 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 identifier
      blobId - The blob ID
      thumbnailBlobId - The thumbnail blob ID
      thumbnailMediaType - The thumbnail media type
      encryptionKey - The encryption key
      mimeType - The mime type
      fileName - The filename
      size - The file size
      caption - The file caption
      renderingType - The rendering type
      correlationId - The correlation identifier
      metadata - The metadata
      senderPrivateKey - Private key of sender
      recipientPublicKey - 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 identifier
      ballotId - The poll ballot identifier
      description - The group poll description
      state - The group poll state
      votingMode - The voting mode
      resultsDisclosureType - The poll results disclosure type
      order - The poll results order (deprecated)
      displayMode - The display mode
      choices - The available vote choices
      participants - The poll participants
      senderPrivateKey - Private key of sender
      recipientPublicKey - 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 identifier
      creator - The group poll creator
      ballotId - The poll ballot identifier
      votes - The votes array
      senderPrivateKey - Private key of sender
      recipientPublicKey - 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 encryption
      uploadResult - result of the upload
      senderPrivateKey - the private key of the sending ID
      recipientPublicKey - 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 decrypted
      privateKey - The private key of the recipient
      publicKey - The public key of the sender
      nonce - 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 data
      secret - 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 data
      secret - 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 decrypted
      recipientPrivateKey - the private key of the receiving ID
      senderPublicKey - the public key of the sending ID
      nonce - 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

      public static EncryptResult encrypt(byte[] data, byte[] privateKey, byte[] publicKey)
      Encrypt data using NaCl asymmetric ("box") encryption.
      Parameters:
      data - the data to be encrypted
      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)
    • encryptFileData

      public static EncryptResult encryptFileData(byte[] data)
      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

      public static EncryptResult encryptFileThumbnailData(byte[] data, byte[] encryptionKey)
      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

      public static byte[] hashEmail(String email)
      Hashes an email address for identity lookup.
      Parameters:
      email - the email address
      Returns:
      the raw hash
    • hashPhoneNo

      public static byte[] hashPhoneNo(String phoneNo)
      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.