Posts

Java - Builder Design Pattern - Example

Builder is a creational pattern that provides an efficient way to build objects. Compared with the traditional way of using multiple constructors, using the Builder pattern can ensure the readability and stability of the code. This mode is particularly useful when creating complex objects. Use Builder pattern in the following situations: When the algorithm for creating a complex object should be independent of the object's components and how they are assembled When the construction process must allow the constructed object to have different representation Advantages 1. It provides a clear disseverment between the construction and representation of an object. 2. It provides better control over the construction process. 3. It fortifies transmuting the internal representation of objects. Implementation   Let's see an example and learn how to implement a builder pattern. Consider a POJO of Employee below. public class Employee { // All final attributes private final S

Spring Boot-Schedule a task with SpringBoot-Download Source Code

Hello everyone, today we will learn how to schedule tasks in Spring Boot using @Scheduled annotation. To schedule jobs in the spring boot application to run periodically, spring boot provides @EnableScheduling and @Scheduled annotations. Add @EnableScheduling to Spring Boot Application class Add @EnableScheduling annotation to your spring boot application class. @EnableScheduling is a Spring Context module annotation. It internally imports the SchedulingConfiguration via the @Import(SchedulingConfiguration.class) instruction @ SpringBootApplication @ EnableScheduling public class KnowledgefactoryScheduler { public static void main( String [] args) { SpringApplication.run(KnowledgefactoryScheduler.class, args); } } 1. Scheduling a Task with Fixed Rate You can schedule a method to be executed at a fixed interval by using fixedRate parameter in the @Scheduled annotation. In the following example, The annotated method

Java - Blowfish Encryption and decryption Example

Image
Blowfish is an encryption method developed by Bruce Schneier in 1993 as an alternative to the DES encryption method. It is significantly faster than DES and provides good encryption speed, although no effective cryptanalysis technique has been found to date. It is one of the first secure block ciphers that is not protected by any patents and is therefore freely available for anyone to use. This is a symmetric block cipher algorithm. Block size: 64 bits Key size: variable size from 32 to 448 bits number of subsections: 18 [P-array] number of rounds: 16 number of substitution blocks: 4 [each with 512 records of 32 bits each] develop  Example 1: Encryption and decryption using Blowfish import java.io.UnsupportedEncodingException; import java.nio.charset.Charset; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.util.Base64; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeExc

Hashing in Java - MD5 ,SHA-1, SHA-256, SHA-384,SHA-512 and PBKDF2

Image
What does Hashing mean? A secure password hash is an encrypted sequence of characters obtained after applying certain algorithms and manipulations on user-provided password, which are generally very weak and easy to guess. Please remember that once this password hash is generated and stored in the database, you can not convert it back to the original password. MD5 hash in Java The MD5 message-digest algorithm is a widely used hash function producing a 128-bit hash value. Although MD5 was initially designed to be used as a cryptographic hash function, it has been found to suffer from extensive vulnerabilities. It can still be used as a checksum to verify data integrity, but only against unintentional corruption. It remains suitable for other non-cryptographic purposes, for example for determining the partition for a particular key in a partitioned database. Example: Java-MD5 to hash a String import java.nio.charset.StandardCharsets; import java.security.MessageDigest; i