diff --git a/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/util/CryptoHelper.java b/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/util/CryptoHelper.java index 4339930..70183fd 100644 --- a/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/util/CryptoHelper.java +++ b/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/util/CryptoHelper.java @@ -15,18 +15,28 @@ import java.security.spec.KeySpec; public class CryptoHelper { + /** + * Creates a javax.crypto.SecretKey from a provided password + * @param pass The password + * @return the generated secret key + */ public static SecretKey createKeyFromPassword(String pass){ try { KeySpec spec = new PBEKeySpec(pass.toCharArray(), "abcdefghijklmnop".getBytes(), 65536, 256); // AES-256 SecretKeyFactory f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); - byte[] key = new byte[0]; - key = f.generateSecret(spec).getEncoded(); + byte[] key = f.generateSecret(spec).getEncoded(); return new SecretKeySpec(key, "AES"); } catch (InvalidKeySpecException | NoSuchAlgorithmException e) { throw new RuntimeException(e); } } + /** + * Saves a java.io.Serializable Object into an encrypted file + * @param obj The object to save + * @param file The Path where to save the file + * @param key The SecretKey (AES) to encrypt the file with + */ public static void serializeEncrypted(Serializable obj, Path file, SecretKey key){ try { if(file.toFile().exists()) file.toFile().delete(); @@ -45,8 +55,14 @@ public class CryptoHelper { } } + /** + * Reads an encrypted file into a java.io.Serializable object. + * @param file The java.nio.Path where the encrypted file is stored. + * @param key The SecretKey (AES) to decrypt the file with + * @return The java.io.Serializable object read from the file. + */ public static Serializable deserializeEncrypted(Path file, SecretKey key) { - Serializable ret = null; + Serializable ret; try { Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); IvParameterSpec iv = new IvParameterSpec("abcdefghijklmnop".getBytes()); diff --git a/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/util/FileHelper.java b/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/util/FileHelper.java index e1ad05b..52e2890 100644 --- a/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/util/FileHelper.java +++ b/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/util/FileHelper.java @@ -7,6 +7,11 @@ import java.util.Base64; public class FileHelper { + /** + * Reads a file and encodes it into Base64 + * @param file The file to read + * @return A Base64 encoded String containing the data of the file + */ public static String encodeFileToBase64(File file){ String encodedfile = null; try (FileInputStream fileInputStreamReader = new FileInputStream(file)){ diff --git a/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/util/Logger.java b/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/util/Logger.java index cad03cc..15c8ee4 100644 --- a/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/util/Logger.java +++ b/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/util/Logger.java @@ -4,6 +4,11 @@ import static de.b00tload.tools.lastfmtospotifyplaylist.LastFMToSpotify.configur public class Logger { + /** + * Logs the provided String to console if the option logging.level in the configuration is higher or equal to the provided priority + * @param string The String to log + * @param priority The minimum logging level with which the provided string should be logged + */ public static void logLn(String string, int priority){ if(Integer.parseInt(configuration.get("logging.level"))>=priority){ System.out.println(string); diff --git a/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/util/PeriodHelper.java b/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/util/PeriodHelper.java index 2c68faf..6ded178 100644 --- a/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/util/PeriodHelper.java +++ b/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/util/PeriodHelper.java @@ -4,6 +4,11 @@ import de.umass.lastfm.Period; public class PeriodHelper { + /** + * Converts a provided String (de.umass.lastfm.Period.getString()) into the corresponding de.umass.lastfm.Period + * @param string The value to be converted into a de.umass.lastfm.Period + * @return The converted de.umass.lastfm.Period, defaults to de.umass.lastfm.Period.ONE_MONTH + */ public static Period getPeriodByString(String string){ for(Period p : Period.values()){ if(p.getString().equalsIgnoreCase(string)) return p; diff --git a/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/util/TimeHelper.java b/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/util/TimeHelper.java index 8e6ba74..3cf12c6 100644 --- a/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/util/TimeHelper.java +++ b/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/util/TimeHelper.java @@ -5,19 +5,33 @@ import java.time.ZoneId; public class TimeHelper { + /** + * Gets the hours of the UTC offset from a LocalDateTime, which is asserted to be at the system default Timezone (ZoneId.systemDefault()) + * @param now The java.time.LocalDateTime for which the offset should be calculated + * @return The hours of offset from UTC + */ public static int getUTCOffsetHours(LocalDateTime now){ - return (int) Math.floor((double) now.atZone(ZoneId.systemDefault()).getOffset().getTotalSeconds()/3600); } + /** + * Gets the minutes of the UTC offset from a LocalDateTime, which is asserted to be at the system default Timezone (ZoneId.systemDefault()) + * @param now The java.time.LocalDateTime for which the offset should be calculated + * @return The minutes of offset from UTC + */ public static int getUTCOffsetMinutes(LocalDateTime now){ return (now.atZone(ZoneId.systemDefault()).getOffset().getTotalSeconds()/60); } + /** + * Generates an ISO 8601 complying String containing the UTC offset from a LocalDateTime, which is asserted to be at the system default Timezone (ZoneId.systemDefault()) + * @param now The java.time.LocalDateTime for which the offset should be calculated + * @return an ISO 8601 complying String containing the UTC offset (e.g. "+01:00") + */ public static String getUTCOffset(LocalDateTime now){ int hour = getUTCOffsetHours(now); String h = (hour == Math.abs(hour) ? "+" : "-") + (String.valueOf(Math.abs(hour)).length() == 1 ? "0" + Math.abs(hour) : String.valueOf(Math.abs(hour))); - int min = Math.abs(getUTCOffsetMinutes(now))-(Math.abs(hour)*60); + int min = Math.abs(getUTCOffsetMinutes(now))%(Math.abs(hour)); String m = (String.valueOf(Math.abs(min)).length() == 1 ? "0" + Math.abs(min) : String.valueOf(Math.abs(min))); return h + ":" + m; } diff --git a/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/util/TokenHelper.java b/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/util/TokenHelper.java index 8197b21..b9e434a 100644 --- a/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/util/TokenHelper.java +++ b/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/util/TokenHelper.java @@ -8,14 +8,27 @@ import static de.b00tload.tools.lastfmtospotifyplaylist.LastFMToSpotify.USER_HOM import static de.b00tload.tools.lastfmtospotifyplaylist.LastFMToSpotify.configuration; public class TokenHelper { + + /** + * Manages saving a se.michaelthelin.spotify.model_objects.credentials.AuthorizationCodeCredentials into "~/.lfm2s/spotify.lfm2scred" using de.b00tload.tools.lastfmtospotifyplaylist.util.CryptoHelper.serializeEncrypted(...) + * @param cred The se.michaelthelin.spotify.model_objects.credentials.AuthorizationCodeCredentials to be saved + */ public static void saveTokens(AuthorizationCodeCredentials cred) { CryptoHelper.serializeEncrypted(cred, Path.of(USER_HOME, "/.lfm2s/spotify.lfm2scred"), CryptoHelper.createKeyFromPassword(configuration.get("cache.crypto"))); } + /** + * Manages retrieving a se.michaelthelin.spotify.model_objects.credentials.AuthorizationCodeCredentials from "~/.lfm2s/spotify.lfm2scred" using de.b00tload.tools.lastfmtospotifyplaylist.util.CryptoHelper.deserializeEncrypted(...) + * @return The retrieved se.michaelthelin.spotify.model_objects.credentials.AuthorizationCodeCredentials + */ public static AuthorizationCodeCredentials fetchTokens() { return (AuthorizationCodeCredentials) CryptoHelper.deserializeEncrypted(Path.of(USER_HOME, "/.lfm2s/spotify.lfm2scred"), CryptoHelper.createKeyFromPassword(configuration.get("cache.crypto"))); } + /** + * Checks whether the saved spotify AuthorizationCodeCredentials at "~/.lfm2s/spotify.lfm2scred" exist + * @return true if file exists, false if not + */ public static boolean existsTokens(){ return Path.of(USER_HOME, "/.lfm2s/spotify.lfm2scred").toFile().exists(); }