我想兴奋地构建一个函数,它生成一个带有密钥的HMAC,就像这个站点提供的:
http://www.freeformatter.com/hmac-generator.html
java 8 lib只提供MessageDigest和KeyGenerator,它们最多只支持SH256.
谷歌也没有给我任何结果来生成HMAC.
有人知道实施吗?
我有这个代码生成一个普通的SH256,但我想这对我没什么帮助:
public static String get_SHA_512_SecurePassword(String passwordToHash) throws Exception {
String generatedPassword = null;
MessageDigest md = MessageDigest.getInstance("SHA-512");
byte[] bytes = md.digest(passwordToHash.getBytes("UTF-8"));
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
}
generatedPassword = sb.toString();
System.out.println(generatedPassword);
return generatedPassword;
}
Run Code Online (Sandbox Code Playgroud) 我有以下代码:
Map<String, Map<Double, String>> map = new HashMap<>();
Map<Double,String> Amap = new HashMap<>();
map.put(getValuesTypes.FUT(), HERE);
Run Code Online (Sandbox Code Playgroud)
而不是首先创建一个Map并将其放在"HERE",我正在寻找一个像我可以在List那里使用的功能,这样我就Arrays.asList(...)
可以进入"Here" ?.asMap({1.0,"A"}, {2.0,"B"})
代码我的问题是什么:
@SpringBootApplication
public class Application {
private static final Logger log = LoggerFactory.getLogger(Application.class);
public static void main(String args[]) {
SpringApplication.run(Application.class);
}
@Bean
public Object test(RestTemplate restTemplate) {
Quote quote = restTemplate.getForObject(
"http://gturnquist-quoters.cfapps.io/api/random", Quote.class);
log.info(quote.toString());
return new Random();
}
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}
@Bean
public CommandLineRunner run(RestTemplate restTemplate) throws Exception {
return args -> {
Quote quote = restTemplate.getForObject(
"http://gturnquist-quoters.cfapps.io/api/random", Quote.class);
log.info(quote.toString());
};
}
}
Run Code Online (Sandbox Code Playgroud)
我是Spring的新手。据我了解,@ Bean注释负责将对象保存在IoC容器中,对吗?
如果是这样:首先是否收集了所有带有@Bean的方法,然后执行了这些方法?
在我的示例中,我添加了一个方法test()与run()相同,但是返回一个对象(Random())。结果是相同的,因此可以使用CommandLineRunner和Object。
是否有理由为什么要返回CommandLineRunner即使用run()之类的语法?
而且:到目前为止,我还没有看到将方法移到容器的优势。为什么不执行它呢?
谢谢!
我在我的程序中有这个代码,需要使用jUnit进行测试
void deleteCustomer(String name) throws UnknownCustomerException,
AccountNotEmptyException {
if (name == null) {
throw new NullPointerException();
} else if (!exists(name)) {
throw new UnknownCustomerException();
} else if (getCustomer(name).deletable()) {
customerList.remove(getCustomer(name));
}
}
Run Code Online (Sandbox Code Playgroud)
我想我可以在一个JUnit方法中测试它
@Test
public void createCustomer(){
System.out.println("createCustomerTest");
try {
element.createCustomer(null);
//fail("Expected an IndexOutOfBoundsException to be thrown");
} catch (NullPointerException anIndexOutOfBoundsException) {
assertTrue(anIndexOutOfBoundsException.getMessage().equals("NullPointerException"));
}
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我已经尝试过实施NPE失败了.如何在一个JUnit方法中检查几个异常?我在网上检查了一些操作方法,但也失败了.
我只是好奇我是否可以切换它
for (String arg : args) {
int number = Integer.parseInt(arg);
int checksum = 0;
for (int digits = 0; digits < 10; digits++) {
checksum += number % 10;
number /= 10;
}
System.out.println(checksum);
}
}
Run Code Online (Sandbox Code Playgroud)
到一个管道Java流(只有那个条件,没有递归方法等).
我的第一次尝试是
public static void main(String... args) {
Stream.of(args)
.?
}
Run Code Online (Sandbox Code Playgroud)
然后以某种方式使用如下函数:
class FunctionTest implements Function<String, Integer> {
@Override
public Integer apply(String s) {
int number = Integer.parseInt(s);
int checksum = 0;
IntStream.range(1,10).reduce(number, ()-> ?)
checksum += number % 10;
number /= 10; …Run Code Online (Sandbox Code Playgroud) java ×4
dictionary ×1
for-loop ×1
hmac ×1
java-stream ×1
junit ×1
list ×1
replace ×1
spring ×1