/** * If the file is a Directory, calculate the checksum of all files in this directory (one level) * Else, calculate the checksum of the file matching extensions * * @param filePath file or folder * @param extensions of files to calculate checksum of * @return checksum */ public static String calculateChecksum(@NotNull Path filePath, String... extensions) { if (filePath == null || !Files.exists(filePath)) { throw new CouchmoveException("File is null or doesn't exists"); } if (Files.isDirectory(filePath)) { return directoryStream(filePath, extensions) .sorted(Comparator.comparing(path -> path.getFileName().toString())) .map(FileUtils::calculateChecksum) .reduce(String::concat) .map(DigestUtils::sha256Hex) .orElse(null); } try { return DigestUtils.sha256Hex(toByteArray(filePath.toUri())); } catch (IOException e) { throw new CouchmoveException("Unable to calculate file checksum '" + filePath.getFileName().toString() + "'"); } }
@Override public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { JobDetail jobDetail = jobExecutionContext.getJobDetail(); _logger.info("Cron triggered for {0}", jobDetail.getDescription()); JobDataMap dataMap = jobDetail.getJobDataMap(); try { //Creating entry in redis to avoid duplicate job scheduling MemcacheService cache = AppFactory.get().getMemcacheService(); String jobKey = "SJOB_" + DigestUtils.md5Hex(dataMap.getString("url")); if (cache.get(jobKey) == null) { HttpUtil .connectMulti(HttpUtil.GET, ConfigUtil.get("task.url") + dataMap.getString("url"), null, null, null, null); cache.put(jobKey, true, 1800); } else { _logger.warn("Job with url {0} is already scheduled. Doing nothing!!", dataMap.getString("url")); } } catch (Exception e) { _logger.warn("Scheduled job failed for url {0} with exception {1}", dataMap.getString("url"), e.getMessage(), e); } }
public static final String calculate(Map<String, ? extends Object> parameters, String appsecret) { TreeMap<String, Object> params = new TreeMap(parameters); params.remove("sign"); params.put("appsecret", appsecret); StringBuilder stringBuilder = new StringBuilder(); Iterator var4 = params.entrySet().iterator(); while(var4.hasNext()) { Map.Entry<String, Object> entry = (Map.Entry)var4.next(); stringBuilder.append(((String)entry.getKey()).trim()); stringBuilder.append("="); stringBuilder.append(entry.getValue().toString().trim()); stringBuilder.append("&"); } stringBuilder.deleteCharAt(stringBuilder.length() - 1); return DigestUtils.sha1Hex(stringBuilder.toString()); }
/** * Computes the digest of the contents of the file this database belongs to. * * @return a digest, representing the contents of the file * @throws IOException in the case of an error during IO operations */ private String computeFileDigest() throws IOException { final BasicFileAttributes attr = Files.readAttributes(Paths.get(fileDatabase.getFileName()), BasicFileAttributes.class); return DigestUtils.sha512Hex(fileDatabase.getFileName() + attr.lastModifiedTime() + attr.size()); }
@Override public void handle(CrawlerResult crawlerResult, CrawlerRequest request) { PrintWriter pw = null; try { pw = new PrintWriter(new OutputStreamWriter( new FileOutputStream(getFile(path + DigestUtils.md5Hex(request.getUrl()) + ".html")),"UTF-8")); Map<String , Object> data = crawlerResult.getAllData(); for (String key : data.keySet()) { pw.println(key + ": \t" + data.get(key)); } pw.flush(); } catch (IOException e) { logger.error("error occurred : {}", e); }finally { pw.close(); } }
@NotNull protected String computeHash(String password, String salt){ /* Given an hash function "f", we have f(password) = hash the main point is that, even if one knows the details of "f" and has the hash, then it is extremely difficult to derive the input password, ie find a function "g" such that g(hash) = password */ String combined = password + salt; /* The password is combined with a "salt" to avoid: 1) two users with same password having same hash. Even if one password gets compromised, an attacker would have no way to know if any other user has the same password 2) make nearly impossible a brute force attack based on "rainbow tables", ie pre-computed values of all hashes from password strings up to length N. This is because now the hashed string will be at least the length of the salt (eg 26) regardless of the length of the password. Note: DigestUtils from commons-codec library is just an utility to simplify the usage of Java API own MessageDigest class */ String hash = DigestUtils.sha256Hex(combined); return hash; }
@RequestMapping(value = "/blur", method = RequestMethod.GET) @ResponseBody public ResponseEntity<InputStreamResource> blur(@RequestParam("source") String sourceUrl, HttpServletResponse response) { if (!StringUtils.startsWithAny(sourceUrl, ALLOWED_PREFIX)) { return ResponseEntity.badRequest().build(); } String hash = DigestUtils.sha1Hex(sourceUrl); try { ImageInfo info = readCached(hash); if (info == null) { info = renderImage(sourceUrl); if (info != null) { saveCached(hash, info); } } if (info != null) { return ResponseEntity.ok() .contentLength(info.contentLength) .contentType(MediaType.IMAGE_JPEG) .body(new InputStreamResource(info.inputStream)); } } catch (IOException e) { // fall down } return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); }
/** * 生成文件摘要 * * @param strFilePath 文件路径 * @param file_digest_type 摘要算法 * @return 文件摘要结果 */ public static String getAbstract(String strFilePath, String file_digest_type) throws IOException { PartSource file = new FilePartSource(new File(strFilePath)); if (file_digest_type.equals("MD5")) { return DigestUtils.md5Hex(file.createInputStream()); } else if (file_digest_type.equals("SHA")) { return DigestUtils.sha256Hex(file.createInputStream()); } else { return ""; } }
/** * 获取推流地址 如果不传key和过期时间,将返回不含防盗链的url */ public static String getPushUrl(StreamUrlConfig config) { String liveCode = getStreamId(config.getBizId(), config.getRoomId(), config.getUserId(), config.getDataType()); if(isNotBlank(config.getKey()) && config.getExpireTime() != null){ String txTime = Long.toHexString(config.getExpireTime().getTime()/1000).toUpperCase(); String input = new StringBuilder().append(config.getKey()).append(liveCode) .append(txTime).toString(); try { String txSecret = DigestUtils.md5Hex(input.getBytes("UTF-8")); return "rtmp://"+config.getBizId()+".livepush.myqcloud.com/live/"+liveCode+"?bizid="+config.getBizId()+"&txSecret="+txSecret+"&txTime="+txTime; } catch (Exception e) { Throwables.propagate(e); } } return "rtmp://"+config.getBizId()+".livepush.myqcloud.com/live/"+liveCode; }
/** * 一个简单的签名认证,规则: * 1. 将请求参数按ascii码排序 * 2. 拼接为a=value&b=value...这样的字符串(不包含sign) * 3. 混合密钥(secret)进行md5获得签名,与请求的签名进行比较 */ private boolean validateSign(HttpServletRequest request) { String requestSign = request.getParameter("sign");//获得请求签名,如sign=19e907700db7ad91318424a97c54ed57 if (StringUtils.isEmpty(requestSign)) { return false; } List<String> keys = new ArrayList<String>(request.getParameterMap().keySet()); keys.remove("sign");//排除sign参数 Collections.sort(keys);//排序 StringBuilder sb = new StringBuilder(); for (String key : keys) { sb.append(key).append("=").append(request.getParameter(key)).append("&");//拼接字符串 } String linkString = sb.toString(); linkString = StringUtils.substring(linkString, 0, linkString.length() - 1);//去除最后一个'&' String secret = "Potato";//密钥,自己修改 String sign = DigestUtils.md5Hex(linkString + secret);//混合密钥md5 return StringUtils.equals(sign, requestSign);//比较 }
@Override public void push(Request request, ISpider spider) { Jedis jedis = pool.getResource(); if (Const.HttpMethod.POST == request.getMethod() || !isDuplicate(request, spider)) { log.debug("push to queue {}", request.getUrl()); try { jedis.rpush(getQueueKey(spider), request.getUrl()); String field = DigestUtils.md5Hex(request.getUrl()); byte[] data=SerializationUtils.serialize(request); jedis.hset((ITEM_PREFIX + spider.getName()).getBytes(), field.getBytes(), data); } finally { jedis.close(); } } }
private void openJscriptStepEditor() { Project project = dbo.getProject(); try { // Create private folder if it does not exist FileUtils.createFolderIfNotExist(project.getDirPath(), "_private"); String fileName = FileUtils.createTmpFileWithUTF8Data( project.getDirPath(), "_private" + "/" + Base64.encodeBase64URLSafeString(DigestUtils.sha1(dbo.getQName())) + " " + dbo.getName() + "." + JSCRIPT_STEP_EDITOR, ((SimpleStep) dbo).getExpression() ); studio.createResponse( new OpenEditableEditorActionResponse( project.getQName() + "/" + "_private" + "/" + fileName, JSCRIPT_STEP_EDITOR ).toXml(studio.getDocument(), getObject().getQName()) ); } catch (Exception e) { } }
public static String sha1(String str){ String s = null; try { byte[] data = str.getBytes("UTF-8"); s = DigestUtils.sha1Hex(data); }catch (Exception ex){ ex.printStackTrace(); } return s; }
String createTupasVerificationMac(String version, String timestamp, String idNbr, String origStamp, String custName, String keyVers, String algorithm, String custId, String custType, String sharedSecret) { String tupasString = version + "&" + timestamp + "&" + idNbr + "&" + origStamp + "&" + custName + "&" + keyVers + "&" + algorithm + "&" + custId + "&" + custType + "&" + sharedSecret + "&"; logger.debug("Generated Tupas response MAC string:\n{}", tupasString); try { return DigestUtils.sha256Hex(tupasString.getBytes("ISO-8859-1")).toUpperCase(); } catch (UnsupportedEncodingException e) { logger.error("Unable to calculate tupas response verification MAC", e); return null; } }
/** * 根据SinpleField列表生成签名 * * 加2个参数 delimiter,caseConvert * * @param fieldPaireds SinpleField的列表 * @param salt partnerApiKey * @return 生成的签名字符串 */ private static String makeSignBySinpleFieldList(List<FieldPaired> fieldPaireds, String salt, Boolean excludeKeyParameter, SignatureAlgorithmic algorithmic, String saltParameterPrefix, String charset, CaseControl caseControl, String delimiter) { List<String> list = fieldPaireds.stream() .sorted(new AsciiSortedComparator<>(FieldPaired::getProperty)).map( FieldPaired::toString).collect(Collectors.toList()); //在对象上添加特殊属性, 当不排除时添加 if (!excludeKeyParameter) { if (StringUtils.isEmpty(saltParameterPrefix)) { throw new RuntimeException("指定了需要添加KEY=到salt前面, 却没有指定前前缀, 请检查官方文档,再做相应调整"); } list.add(saltParameterPrefix + salt); } // 未加密字符串 String unencrypted = ""; try { unencrypted = new String(String.join(delimiter, list).getBytes(), charset); //将salt添加到最后面 if (!StringUtils.isEmpty(salt)) { if (excludeKeyParameter) { unencrypted += salt; } } log.debug("Unencrypted String is: {}", unencrypted); } catch (Exception e) { e.printStackTrace(); } String result = ""; switch (algorithmic) { case MD2: result = DigestUtils.md2Hex(unencrypted); break; case MD5: result = DigestUtils.md5Hex(unencrypted); break; case SHA1: result = DigestUtils.sha1Hex(unencrypted); break; case SHA256: result = DigestUtils.sha256Hex(unencrypted); break; case SHA384: result = DigestUtils.sha384Hex(unencrypted); break; case SHA512: result = DigestUtils.sha512Hex(unencrypted); break; default: throw new RuntimeException("不支持的签名类型"); } if (null != caseControl) { switch (caseControl) { case TO_LOWER_CASE: result = result.toLowerCase(); break; case TO_UPPER_CASE: result = result.toUpperCase(); break; } } log.debug("Encrypted Signature is: {}", result); return result; }
/** * Check if the MD5s of manifest.json and manifest.checksum equal * if so, pull out the manifest file and map it into a POJO * @return inventoryManifestStorage InventoryManifest, which stores all the elements of the manifest.json file */ public InventoryManifest getInventoryManifest() throws Exception { // Get manifest.json and transfer it to String GetObjectRequest requestJson = new GetObjectRequest(bucketName, bucketKeyJson); S3Object jsonObject = s3Client.getObject(requestJson); String jsonFile = inputStreamToString(jsonObject.getObjectContent()); jsonObject.close(); // Get manifest.checksum and transfer it to String with no whitespace GetObjectRequest requestChecksum = new GetObjectRequest(bucketName, bucketKeyChecksum); S3Object checksumObject = s3Client.getObject(requestChecksum); String expectedChecksum = inputStreamToString(checksumObject.getObjectContent()) .replaceAll("\\s",""); checksumObject.close(); // Compare manifest.json and manifest.checksum's MD5 value String actualChecksum = DigestUtils.md5Hex(jsonFile); if (!actualChecksum.equals(expectedChecksum)) { throw new ChecksumMismatchException (expectedChecksum, actualChecksum); } return mapper.readValue(jsonFile, InventoryManifest.class); }
private void openJscriptStepEditor(IProject project) { Step step = (Step) this.getObject(); IFile file = project.getFile("/_private/" + Base64.encodeBase64URLSafeString(DigestUtils.sha1(step.getQName())) + " " + step.getName()); IWorkbenchPage activePage = PlatformUI .getWorkbench() .getActiveWorkbenchWindow() .getActivePage(); if (activePage != null) { try { activePage.openEditor(new JscriptStepEditorInput(file,step), "com.twinsoft.convertigo.eclipse.editors.jscript.JscriptStepEditor"); } catch(PartInitException e) { ConvertigoPlugin.logException(e, "Error while loading the step editor '" + step.getName() + "'"); } } }
private boolean checkHash(String p_190113_1_, File p_190113_2_) { try { String s = DigestUtils.sha1Hex((InputStream)(new FileInputStream(p_190113_2_))); if (p_190113_1_.isEmpty()) { LOGGER.info("Found file {} without verification hash", new Object[] {p_190113_2_}); return true; } if (s.toLowerCase().equals(p_190113_1_.toLowerCase())) { LOGGER.info("Found file {} matching requested hash {}", new Object[] {p_190113_2_, p_190113_1_}); return true; } LOGGER.warn("File {} had wrong hash (expected {}, found {}).", new Object[] {p_190113_2_, p_190113_1_, s}); } catch (IOException ioexception) { LOGGER.warn("File {} couldn\'t be hashed.", new Object[] {p_190113_2_, ioexception}); } return false; }
private String __buildJsApiConfigStr(String appId, String jsapiTicket, String url, String timestamp, String noncestr, boolean debug) throws Exception { String _signature = "jsapi_ticket=" + jsapiTicket + "&" + "noncestr=" + noncestr + "&" + "timeStamp=" + timestamp + "&" + "url=" + url; _signature = DigestUtils.sha1Hex(_signature); // JSONObject _json = new JSONObject(); _json.put("debug", debug); _json.put("appId", appId); _json.put("timestamp", timestamp); _json.put("nonceStr", noncestr); _json.put("signature", _signature); _json.put("jsApiList", new String[]{"chooseWXPay"}); // return _json.toJSONString(); }
/** * 用&串接arr参数,生成sha1 digest */ public static String genWithAmple(String... arr) { Arrays.sort(arr); StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { String a = arr[i]; sb.append(a); if (i != arr.length - 1) { sb.append('&'); } } return DigestUtils.sha1Hex(sb.toString()); }
/** * 签名字符串 * @param text 需要签名的字符串 * @param sign 签名结果 * @param key 密钥 * @param input_charset 编码格式 * @return 签名结果 */ public static boolean verify(String text, String sign, String key, String input_charset) { text = text + key; String mysign = DigestUtils.md5Hex(getContentBytes(text, input_charset)); if(mysign.equals(sign)) { return true; } else { return false; } }
public static String hash(String code) { return DigestUtils.sha256Hex(code); }
/** * 将字符串 SHA 加密 */ public static String encryptSHA(String str) { return DigestUtils.sha1Hex(str); }
@Override public void process(ResultItems resultItems, Task task) { if(resultItems.get("url")!=null){ try { PrintWriter printWriter = new PrintWriter(new FileWriter(this.getFile(path + DigestUtils.md5Hex(resultItems.getRequest().getUrl()) + ".json"))); printWriter.write(JSON.toJSONString(resultItems.getAll())); printWriter.close(); }catch (IOException e){ e.printStackTrace(); } } }
public String readNewHash() { if (newHash != null) return newHash; try { FileInputStream fis = new FileInputStream(owner.getUt2004Map()); newHash = DigestUtils.md5Hex(fis); fis.close(); } catch (Exception e) { severe("Failed to create MD5 hash from: " + owner.getUt2004Map().getAbsolutePath()); e.printStackTrace(); return null; } return newHash; }
private boolean walk(Iterator<S3ObjectSummary> iter, ObjectId file, String path) throws IOException { byte[] content; byte[] newHash; LOG.debug("Start processing file: {}", path); try (DigestInputStream is = new DigestInputStream(repository.open(file).openStream(), DigestUtils.getMd5Digest())) { // Get content content = IOUtils.toByteArray(is); // Get hash newHash = is.getMessageDigest().digest(); } if (isUploadFile(iter, path, Hex.encodeHexString(newHash))) { LOG.info("Uploading file: {}", path); ObjectMetadata bucketMetadata = new ObjectMetadata(); bucketMetadata.setContentMD5(Base64.encodeAsString(newHash)); bucketMetadata.setContentLength(content.length); // Give Tika a few hints for the content detection Metadata tikaMetadata = new Metadata(); tikaMetadata.set(Metadata.RESOURCE_NAME_KEY, FilenameUtils.getName(FilenameUtils.normalize(path))); // Fire! try (InputStream bis = TikaInputStream.get(content, tikaMetadata)) { bucketMetadata.setContentType(TIKA_DETECTOR.detect(bis, tikaMetadata).toString()); s3.putObject(bucket.getName(), path, bis, bucketMetadata); return true; } } LOG.info("Skipping file (same checksum): {}", path); return false; }
@RequestMapping(value = "/create", method = { RequestMethod.POST }) public String crea(@ModelAttribute User user) { Role userRole = new Role(); userRole.setId(Role.USER_ROLE_ID); user.getRoles().add(userRole); user.setPassword(DigestUtils.md5Hex(user.getPassword())); userService.create(user); // authenticate the user and redirect on the welcome page new AuthenticationHolder().updateUser(userService.findByPK(user.getId())); return "redirect:/dashboard"; }
public boolean send() throws Exception { String url = host + postPath; String postBody = rootJson.toString(); String sign = DigestUtils.md5Hex("POST" + url + postBody + appMasterSecret); url = url + "?sign=" + sign; HttpPost post = new HttpPost(url); post.setHeader("User-Agent", USER_AGENT); StringEntity se = new StringEntity(postBody, "UTF-8"); post.setEntity(se); // Send the post request and get the response HttpResponse response = client.execute(post); int status = response.getStatusLine().getStatusCode(); BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); StringBuffer result = new StringBuffer(); String line = ""; while ((line = rd.readLine()) != null) { result.append(line); } if (status == 200) { LOGGER.info("Notification sent successfully."); return true; } else { LOGGER.error("Failed to send the notification! Result:{}", result); return false; } }
private Function<MessageDigest, Optional<String>> computeDigest(final InputStream stream) { return algorithm -> { try { final String digest = getEncoder().encodeToString(DigestUtils.updateDigest(algorithm, stream).digest()); stream.close(); return of(digest); } catch (final IOException ex) { LOGGER.error("Error computing digest: {}", ex.getMessage()); } return empty(); }; }
@Override protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); try { Resource resource = request.getResource(); if (resource != null && configurationProvider.hasConfig(resource.getResourceType())) { JSONObject filteredContentMap = new JSONObject(); TemplateContentModel templateContentModel = (TemplateContentModel) contentModelFactoryService.getContentModel(request, response); boolean clientAccessible = (Boolean) templateContentModel.get(CONFIG_PROPERTIES_KEY + DOT + XK_CLIENT_ACCESSIBLE_CP); if (clientAccessible) { // get list of contexts Configuration configuration = configurationProvider.getFor(resource.getResourceType()); Collection<String> props = configuration.asStrings(XK_CLIENT_MODEL_PROPERTIES_CP, Mode.MERGE); String[] contexts = props.toArray(new String[0]); // get content model json with the XK_CLIENT_MODEL_PROPERTIES_CP contexts filteredContentMap = templateContentModel.toJSONObject(contexts); // add component id String componentContentId = DigestUtils.md5Hex(resource.getPath()); filteredContentMap.put(XK_CONTENT_ID_CP, componentContentId); } // add component list with clientaccessible as true on the resource page filteredContentMap.put(PAGE_COMPONENT_RESOURCE_LIST_AN, getComponentList(resource)); out.write(JSONObject.toJSONString(filteredContentMap)); } else { out.write(new JSONObject().toJSONString()); } } catch (Exception ew) { throw new ServletException(ew); } }
@Override public void save(String mobile, String password){ UserEntity user = new UserEntity(); user.setMobile(mobile); user.setUsername(mobile); user.setPassword(DigestUtils.sha256Hex(password)); user.setCreateTime(new Date()); userDao.save(user); }
@RequestMapping(value = "/user/reset/password", method = { RequestMethod.POST }) public String userResetPassword(@ModelAttribute User user) { String originalPassword = user.getPassword(); User persistentUser = userService.findByUsername(user.getUsername()); persistentUser.setPassword(DigestUtils.md5Hex(originalPassword)); persistentUser.setPasswordExpired(false); userService.update(persistentUser); return "redirect:/login"; }
/** * 签名字符串 * * @param text 需要签名的字符串 * @param sign 签名结果 * @param key 密钥 * @param input_charset 编码格式 * @return 签名结果 */ public static boolean verify(String text, String sign, String key, String input_charset) { text = text + key; String mysign = DigestUtils.md5Hex(getContentBytes(text, input_charset)); if (mysign.equals(sign)) { return true; } else { return false; } }
/** * Create nonce string. * * @return the nonce */ public static String createNonce() { final String fmtDate = ZonedDateTime.now().toString(); final SecureRandom rand = new SecureRandom(); final Integer randomInt = rand.nextInt(); return DigestUtils.md5Hex(fmtDate + randomInt); }
public static String sha1(List<String> contents) { MessageDigest md = DigestUtils.getSha1Digest(); for (String content : contents) { md.update(content.getBytes(StandardCharsets.UTF_8)); } return Hex.encodeHexString(md.digest()); }
private <T extends QCloudResponse> Map<String, Object> getLiveRequestParams(QCloudRequest<T> request){ String timestamp = Long.toHexString(System.currentTimeMillis()/1000); Map<String, Object> params = request.getTextParams(); params.put("cmd", liveConfig.getSdkAppId());//即直播APPID,用于区分不同客户的身份 params.put("t", timestamp); String sign = DigestUtils.md5Hex(liveConfig.getKey()+timestamp); params.put("sign", sign); params.putAll(request.getTextParams()); return params; }
private String md5(File file) throws IOException { InputStream inp = new FileInputStream(file); try { return DigestUtils.md5Hex(inp); } finally { Closeables.close(inp, true); } }
public void setPassword(String password) { if(password.length()<16) { this.password =DigestUtils.shaHex(password); } else { this.password =password; } }
/** * Gets file sha256. * * @return sha256 checksum or null is file does not exist. * @throws MbedCloudException * if a problem happens during checksum calculation */ public @Nullable String getSha256Checksum() throws MbedCloudException { return getChecksum(new ChecksumFunction() { @Override public String determineCheckSum(InputStream is) throws IOException { return DigestUtils.sha256Hex(is); } }); }
@Override public User login(User user) { Map<String, Object> params = new HashMap<>(); params.put("username", user.getUsername()); params.put("password", DigestUtils.md5Hex(user.getPassword())); user.setId(10000L); user.setName("xiao"); return user; }