Java Code Examples for org.apache.commons.codec.digest.DigestUtils

The following are top voted examples for showing how to use org.apache.commons.codec.digest.DigestUtils. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: logistimo-web-service   File: CronJob.java   View source code 6 votes vote down vote up
@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);
  }

}
 
Example 2
Project: couchmove   File: FileUtils.java   View source code 6 votes vote down vote up
/**
 * 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() + "'");
    }
}
 
Example 3
Project: dooo   File: Signs.java   View source code 6 votes vote down vote up
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());
}
 
Example 4
Project: hygene   File: FileMetadata.java   View source code 6 votes vote down vote up
/**
 * 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());
}
 
Example 5
Project: fastcrawler   File: FileResultHandler.java   View source code 6 votes vote down vote up
@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();
        }
}
 
Example 6
Project: wakeup-qcloud-sdk   File: QCloudStreamUrlUtil.java   View source code 6 votes vote down vote up
/**
 * 获取推流地址 如果不传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;
}
 
Example 7
Project: pingguopai   File: WebMvcConfigurer.java   View source code 6 votes vote down vote up
/**
 * 一个简单的签名认证,规则:
 * 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);//比较
}
 
Example 8
Project: crawler   File: RedisScheduler.java   View source code 6 votes vote down vote up
@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();
	        }
	}
   }
 
Example 9
Project: convertigo-engine   File: StepView.java   View source code 6 votes vote down vote up
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) {
    }
}
 
Example 10
Project: e-identification-tupas-idp-public   File: SessionParserService.java   View source code 6 votes vote down vote up
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;
    }
}
 
Example 11
Project: s3-inventory-usage-examples   File: InventoryManifestRetriever.java   View source code 6 votes vote down vote up
/**
 * 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);
}
 
Example 12
Project: convertigo-eclipse   File: StepTreeObject.java   View source code 6 votes vote down vote up
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() + "'");
		} 
	}
}
 
Example 13
Project: epay   File: MD5.java   View source code 5 votes vote down vote up
/**
 * 签名字符串
 * @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;
	}
}
 
Example 14
Project: FashionSpider   File: MyJSONPipeline.java   View source code 5 votes vote down vote up
@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();
        }
    }
}
 
Example 15
Project: Pogamut3   File: NavMeshMakerStep_ExportMapHash.java   View source code 5 votes vote down vote up
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;	
}
 
Example 16
Project: github-bucket   File: RepositoryS3.java   View source code 5 votes vote down vote up
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;
}
 
Example 17
Project: profile-manager   File: LayoutController.java   View source code 5 votes vote down vote up
@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";
}
 
Example 18
Project: raven   File: UmengNotification.java   View source code 5 votes vote down vote up
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;
	}
}
 
Example 19
Project: trellis   File: HttpBasedBinaryService.java   View source code 5 votes vote down vote up
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();
    };
}
 
Example 20
Project: testing_security_development_enterprise_systems   File: UserEJB.java   View source code 5 votes vote down vote up
@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;
}
 
Example 21
Project: AEM   File: PageContentModelToJSONServlet.java   View source code 5 votes vote down vote up
@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);
    }
}
 
Example 22
Project: JuniperBotJ   File: BlurImageController.java   View source code 5 votes vote down vote up
@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();
}
 
Example 23
Project: pds   File: UserServiceImpl.java   View source code 5 votes vote down vote up
@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);
}
 
Example 24
Project: framework   File: AlipayCore.java   View source code 5 votes vote down vote up
/**
 * 生成文件摘要
 *
 * @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 "";
	}
}
 
Example 25
Project: spring-grow   File: UtilityController.java   View source code 5 votes vote down vote up
@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";
}
 
Example 26
Project: framework   File: MD5.java   View source code 5 votes vote down vote up
/**
 * 签名字符串
 *
 * @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;
	}
}
 
Example 27
Project: cas-5.1.0   File: DigestAuthenticationUtils.java   View source code 5 votes vote down vote up
/**
 * 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);
}
 
Example 28
Project: Dragonfly   File: DigestUtil.java   View source code 5 votes vote down vote up
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());
}
 
Example 29
Project: wakeup-qcloud-sdk   File: DefaultQCloudClient.java   View source code 5 votes vote down vote up
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;
}
 
Example 30
Project: Equella   File: FileApiTest.java   View source code 5 votes vote down vote up
private String md5(File file) throws IOException
{
	InputStream inp = new FileInputStream(file);
	try
	{
		return DigestUtils.md5Hex(inp);
	}
	finally
	{
		Closeables.close(inp, true);
	}
}
 
Example 31
Project: KutuphaneOtomasyonSistemi   File: Secimler.java   View source code 5 votes vote down vote up
public void setPassword(String password) {
	if(password.length()<16)
	{
	this.password =DigestUtils.shaHex(password);
	}
	else
	{
		this.password =password;
	}
}
 
Example 32
Project: mbed-cloud-sdk-java   File: DataFile.java   View source code 5 votes vote down vote up
/**
 * 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);
        }
    });
}
 
Example 33
Project: SpringBootStudy   File: UserServiceImpl.java   View source code 5 votes vote down vote up
@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;
}
 
Example 34
Project: Dragonfly   File: SuperWriter.java   View source code 5 votes vote down vote up
@Override
public void run() {
    try {
        MessageDigest pieceM5 = DigestUtils.getMd5Digest();
        int pieceNum;
        ByteBuffer bb, byteBuf = ByteBuffer.allocate(this.pieceSize);
        byteBuf.order(ByteOrder.BIG_ENDIAN);

        while (true) {
            ProtocolContent protocolContent = contQu.poll(waitTime, TimeUnit.SECONDS);
            if (protocolContent == null) {
                logger.warn("taskId:{} get piece timeout", taskId);
                break;
            }
            if (protocolContent.isPieceType()) {
                pieceNum = protocolContent.getPieceNum();
                bb = protocolContent.getContent();

                byteBuf.putInt(bb.limit() | this.pieceSizeBit);
                byteBuf.put(bb);
                byteBuf.put((byte)0x7f);
                bb.clear();
                reusedCache.offer(bb);
                reportPiece(pieceM5, byteBuf, pieceNum);
            } else {
                contQu.put(protocolContent);
                break;
            }
        }
        sucCount.incrementAndGet();
    } catch (Exception e) {
        logger.error("write piece error for taskId:{}", taskId, e);
    } finally {
        downLatch.countDown();
    }
}
 
Example 35
Project: raven   File: AndroidFilecast.java   View source code 5 votes vote down vote up
public String uploadContents(String contents) throws Exception {
	if (!rootJson.has("appkey") || !rootJson.has("timestamp")) {
		throw new Exception("appkey, timestamp needs to be set.");
	}
	// Construct the json string
	JSONObject uploadJson = new JSONObject();
	uploadJson.put("appkey", rootJson.getString("appkey"));
	uploadJson.put("timestamp", rootJson.getString("timestamp"));
	uploadJson.put("content", contents);
	// Construct the request
	String url = host + uploadPath;
	String postBody = uploadJson.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);
	System.out.println("Response Code : " + 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);
	}
	System.out.println(result.toString());
	// Decode response string and get file_id from it
	JSONObject respJson = new JSONObject(result.toString());
	String ret = respJson.getString("ret");
	if (!ret.equals("SUCCESS")) {
		throw new Exception("Failed to upload file");
	}
	JSONObject data = respJson.getJSONObject("data");
	String fileId = data.getString("file_id");
	// Set file_id into rootJson using setPredefinedKeyValue
	setPredefinedKeyValue("file_id", fileId);
	return fileId;
}
 
Example 36
Project: xm-commons   File: MdcUtils.java   View source code 5 votes vote down vote up
/**
 * Generates request id based on UID and SHA-256.
 *
 * @return request identity
 */
public static String generateRid() {
    byte[] encode = Base64.getEncoder().encode(DigestUtils.sha256(UUID.randomUUID().toString()));
    try {
        String rid = new String(encode, StandardCharsets.UTF_8.name());
        rid = StringUtils.replaceChars(rid, "+/=", "");
        return StringUtils.right(rid, RID_LENGTH);
    } catch (UnsupportedEncodingException e) {
        throw new IllegalStateException(e);
    }
}
 
Example 37
Project: profile-manager   File: LayoutController.java   View source code 5 votes vote down vote up
@RequestMapping(value = "/update", method = { RequestMethod.POST })
public String modifica(@ModelAttribute User user) {
   if (isPasswordChanged(user)) {
      user.setPassword(DigestUtils.md5Hex(user.getPassword()));
   }
   userService.update(user);

   // authenticate the user and redirect on the welcome page
   new AuthenticationHolder().updateUser(user);

   return "redirect:/dashboard";
}
 
Example 38
Project: CurseSync   File: Installer.java   View source code 5 votes vote down vote up
@Nullable
private List<FileOverride> getChecksums(File relative, File directory)
{
    ImmutableList.Builder<FileOverride> checksums = new ImmutableList.Builder<>();
    File[] files = directory.listFiles();
    if (files != null)
    {
        for (File f : files)
        {
            if (f.isDirectory())
            {
                List<FileOverride> subResult = getChecksums(relative, f);
                if (subResult == null)
                    return null;
                checksums.addAll(subResult);
            }
            else
            {
                try
                {
                    InputStream digestStream = new FileInputStream(f);
                    String checksum = DigestUtils.md5Hex(digestStream);
                    digestStream.close();
                    checksums.add(new FileOverride(relative.toPath().relativize(f.toPath()).toString(), checksum));
                }
                catch (IOException e)
                {
                    log.error(new FormattedMessageFactory().newMessage("Failed to calculate checksum of file {}!", f.getAbsolutePath()), e);
                    return null;
                }
            }
        }
    }
    return checksums.build();
}
 
Example 39
Project: JtSQL   File: EncryptUtil.java   View source code 5 votes vote down vote up
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;
}
 
Example 40
Project: VASSAL-src   File: ModuleManagerWindow.java   View source code 5 votes vote down vote up
public void cleanupTileCache() {
  final String hstr = DigestUtils.shaHex(
    metadata.getName() + "_" + metadata.getVersion()
  );

  final File tdir = new File(Info.getConfDir(), "tiles/" + hstr);
  if (tdir.exists()) {
    try {
      FileUtils.forceDelete(tdir);
    }
    catch (IOException e) {
      WriteErrorDialog.error(e, tdir);
    }
  }
}