Skip to content

Commit efbe20d

Browse files
committed
add Arithmetic.java and HmacSHA256.java and FullDateFormatUtils
1 parent fb37ace commit efbe20d

File tree

6 files changed

+312
-0
lines changed

6 files changed

+312
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.codingapi.springboot.framework.crypto;
2+
3+
import javax.crypto.Mac;
4+
import javax.crypto.spec.SecretKeySpec;
5+
import java.security.InvalidKeyException;
6+
import java.security.NoSuchAlgorithmException;
7+
8+
public class HmacSHA256 {
9+
10+
public static byte[] sha256(byte[] message,byte[] secret) throws NoSuchAlgorithmException, InvalidKeyException {
11+
Mac sha256_HMAC = Mac.getInstance("HmacSha256");
12+
SecretKeySpec secretKey = new SecretKeySpec(secret,"HmacSha256");
13+
sha256_HMAC.init(secretKey);
14+
return sha256_HMAC.doFinal(message);
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
package com.codingapi.springboot.framework.math;
2+
3+
4+
import java.math.BigDecimal;
5+
import java.math.BigInteger;
6+
import java.math.RoundingMode;
7+
8+
public class Arithmetic {
9+
10+
private BigDecimal value;
11+
12+
private final static int DEFAULT_SCALE = 4;
13+
private final static RoundingMode DEFAULT_ROUNDING_MODE = RoundingMode.HALF_UP;
14+
15+
public static Arithmetic zero(){
16+
return parse(0);
17+
}
18+
19+
public static Arithmetic one(){
20+
return parse(1);
21+
}
22+
23+
public static Arithmetic parse(int value){
24+
return new Arithmetic(value);
25+
}
26+
27+
public static Arithmetic parse(float value){
28+
return new Arithmetic(value);
29+
}
30+
31+
public static Arithmetic parse(double value){
32+
return new Arithmetic(value);
33+
}
34+
35+
public static Arithmetic parse(long value){
36+
return new Arithmetic(value);
37+
}
38+
39+
40+
public Arithmetic(String value) {
41+
this.value = new BigDecimal(value);
42+
}
43+
44+
public Arithmetic(int value) {
45+
this(String.valueOf(value));
46+
}
47+
48+
public Arithmetic(float value) {
49+
this(String.valueOf(value));
50+
}
51+
52+
public Arithmetic(double value) {
53+
this(String.valueOf(value));
54+
}
55+
56+
public Arithmetic(long value) {
57+
this(String.valueOf(value));
58+
}
59+
60+
public Arithmetic add(Arithmetic val){
61+
this.value = this.value.add(val.value);
62+
return this;
63+
}
64+
65+
66+
public Arithmetic add(String val){
67+
this.value = this.value.add(new Arithmetic(val).value);
68+
return this;
69+
}
70+
71+
public Arithmetic add(int val){
72+
this.value = this.value.add(new Arithmetic(val).value);
73+
return this;
74+
}
75+
76+
public Arithmetic add(float val){
77+
this.value = this.value.add(new Arithmetic(val).value);
78+
return this;
79+
}
80+
81+
public Arithmetic add(double val){
82+
this.value = this.value.add(new Arithmetic(val).value);
83+
return this;
84+
}
85+
86+
public Arithmetic add(long val){
87+
this.value = this.value.add(new Arithmetic(val).value);
88+
return this;
89+
}
90+
91+
public Arithmetic sub(Arithmetic val){
92+
this.value =this.value.subtract(val.value);
93+
return this;
94+
}
95+
96+
public Arithmetic sub(String val){
97+
this.value =this.value.subtract(new Arithmetic(val).value);
98+
return this;
99+
}
100+
101+
public Arithmetic sub(int val){
102+
this.value =this.value.subtract(new Arithmetic(val).value);
103+
return this;
104+
}
105+
106+
public Arithmetic sub(float val){
107+
this.value =this.value.subtract(new Arithmetic(val).value);
108+
return this;
109+
}
110+
111+
112+
public Arithmetic sub(double val){
113+
this.value =this.value.subtract(new Arithmetic(val).value);
114+
return this;
115+
}
116+
117+
118+
public Arithmetic sub(long val){
119+
this.value =this.value.subtract(new Arithmetic(val).value);
120+
return this;
121+
}
122+
123+
124+
public Arithmetic mul(Arithmetic val){
125+
this.value = this.value.multiply(val.value);
126+
return this;
127+
}
128+
129+
public Arithmetic mul(String val){
130+
this.value = this.value.multiply(new Arithmetic(val).value);
131+
return this;
132+
}
133+
134+
public Arithmetic mul(int val){
135+
this.value = this.value.multiply(new Arithmetic(val).value);
136+
return this;
137+
}
138+
139+
public Arithmetic mul(float val){
140+
this.value = this.value.multiply(new Arithmetic(val).value);
141+
return this;
142+
}
143+
144+
public Arithmetic mul(double val){
145+
this.value = this.value.multiply(new Arithmetic(val).value);
146+
return this;
147+
}
148+
149+
public Arithmetic mul(long val){
150+
this.value = this.value.multiply(new Arithmetic(val).value);
151+
return this;
152+
}
153+
154+
155+
public Arithmetic div(Arithmetic val){
156+
this.value = this.value.divide(val.value,DEFAULT_SCALE, DEFAULT_ROUNDING_MODE);
157+
return this;
158+
}
159+
160+
public Arithmetic div(String val){
161+
this.value = this.value.divide(new Arithmetic(val).value, DEFAULT_SCALE,DEFAULT_ROUNDING_MODE);
162+
return this;
163+
}
164+
165+
public Arithmetic div(int val){
166+
this.value = this.value.divide(new Arithmetic(val).value,DEFAULT_SCALE, DEFAULT_ROUNDING_MODE);
167+
return this;
168+
}
169+
170+
171+
public Arithmetic div(float val){
172+
this.value = this.value.divide(new Arithmetic(val).value,DEFAULT_SCALE, DEFAULT_ROUNDING_MODE);
173+
return this;
174+
}
175+
176+
public Arithmetic div(double val){
177+
this.value = this.value.divide(new Arithmetic(val).value,DEFAULT_SCALE,DEFAULT_ROUNDING_MODE);
178+
return this;
179+
}
180+
181+
public Arithmetic div(long val){
182+
this.value = this.value.divide(new Arithmetic(val).value, DEFAULT_SCALE,DEFAULT_ROUNDING_MODE);
183+
return this;
184+
}
185+
186+
public BigDecimal getValue() {
187+
return value;
188+
}
189+
190+
public String getStringValue() {
191+
return value.toString();
192+
}
193+
194+
public BigInteger getBigIntegerValue() {
195+
return value.toBigInteger();
196+
}
197+
198+
public int getIntValue() {
199+
return value.intValue();
200+
}
201+
202+
public double getDoubleValue() {
203+
return value.doubleValue();
204+
}
205+
206+
public float getFloatValue() {
207+
return value.floatValue();
208+
}
209+
210+
public long getLongValue() {
211+
return value.longValue();
212+
}
213+
214+
public Arithmetic halfUpScale2(){
215+
return halfUpScale(2);
216+
}
217+
218+
public Arithmetic halfUpScale(int scale){
219+
value = value.setScale(scale,RoundingMode.HALF_UP);
220+
return this;
221+
}
222+
223+
}
224+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.codingapi.springboot.framework.utils;
2+
3+
4+
import java.text.SimpleDateFormat;
5+
import java.util.Date;
6+
7+
public class FullDateFormatUtils {
8+
private final static SimpleDateFormat defaultFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
9+
10+
public static String convert(long timestamp){
11+
return convert(new Date(timestamp));
12+
}
13+
14+
public static String convert(Date date){
15+
return defaultFormat.format(date);
16+
}
17+
}
18+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.codingapi.springboot.framework.crypto;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.springframework.util.Base64Utils;
5+
6+
import java.security.InvalidKeyException;
7+
import java.security.NoSuchAlgorithmException;
8+
9+
import static org.junit.jupiter.api.Assertions.assertEquals;
10+
11+
class HmacSHA256Test {
12+
13+
@Test
14+
void sha256() throws NoSuchAlgorithmException, InvalidKeyException {
15+
String data = Base64Utils.encodeToString(HmacSHA256.sha256("123456".getBytes(),"123456".getBytes()));
16+
assertEquals("uK0Io6VH41gpuCG3U3AwHdjEsGvdd3H5tUGnWRQGhxg=",data);
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.codingapi.springboot.framework.math;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
class ArithmeticTest {
8+
9+
@Test
10+
void test() {
11+
// 1 + 1 x 3 / 4 = 1.25
12+
assertEquals(Arithmetic.one().add(1).mul(3).div(4).getDoubleValue(),1.5);
13+
14+
// 0.1+0.2=0.3
15+
assertEquals(Arithmetic.parse(0.1).add(0.2).getDoubleValue(),0.3);
16+
17+
// (1.5 x 3.1) + (3.4 x 4.5) = 19.95
18+
assertEquals((Arithmetic.parse(1.5).mul(3.1)).add(Arithmetic.parse(3.4).mul(4.5)).getDoubleValue(),19.95);
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.codingapi.springboot.framework.utils;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
6+
import static org.junit.jupiter.api.Assertions.*;
7+
8+
class FullDateFormatUtilsTest {
9+
10+
@Test
11+
void convert() {
12+
long time = 1663590528793L;
13+
String data =FullDateFormatUtils.convert(time);
14+
assertEquals(data,"2022-09-19 20:28:48");
15+
}
16+
}

0 commit comments

Comments
 (0)