Java Code Examples for org.apache.commons.codec.digest.DigestUtils#shaHex()

The following examples show how to use org.apache.commons.codec.digest.DigestUtils#shaHex() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RedisScheduler.java    From webmagic with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized Request poll(Task task) {
    Jedis jedis = pool.getResource();
    try {
        String url = jedis.lpop(getQueueKey(task));
        if (url == null) {
            return null;
        }
        String key = ITEM_PREFIX + task.getUUID();
        String field = DigestUtils.shaHex(url);
        byte[] bytes = jedis.hget(key.getBytes(), field.getBytes());
        if (bytes != null) {
            Request o = JSON.parseObject(new String(bytes), Request.class);
            return o;
        }
        Request request = new Request(url);
        return request;
    } finally {
        pool.returnResource(jedis);
    }
}
 
Example 2
Source File: FlattenJsonBolt.java    From cognition with Apache License 2.0 6 votes vote down vote up
@Override
protected void execute(Tuple input, RecordCollector collector) {
  byte[] bytes = (byte[]) input.getValue(0);
  String record = new String(bytes);
  String sha1Checksum = DigestUtils.shaHex(bytes);

  if (StringUtils.isBlank(record)) {
    // skips blank entries
    logger.info("received blank record");
    return;
  }
  try {
    LogRecord logRecord = new LogRecord(sha1Checksum);
    logRecord.addMetadataValue(SHA1_CHECKSUM, sha1Checksum);
    parseJson(record, logRecord);
    collector.emit(logRecord);
  } catch (Exception e) {
    // Not bubbling up, since it would fail the entire tuple
    // Parsing failure would not be fixed even after a replay...
    logger.error("Failed to process tuple: " + record, e);
  }
}
 
Example 3
Source File: LineRegexReplaceInRegionBolt.java    From cognition with Apache License 2.0 6 votes vote down vote up
@Override
protected void execute(Tuple tuple, RecordCollector collector) {
  byte[] bytes = (byte[]) tuple.getValue(0);
  String record = new String(bytes);
  String sha1Checksum = DigestUtils.shaHex(bytes);

  if (StringUtils.isBlank(record)) {
    // skips blank entries
    logger.info("received blank record");
    return;
  }

  LogRecord logRecord = new LogRecord(sha1Checksum);
  logRecord.addMetadataValue(SHA1_CHECKSUM, sha1Checksum);

  String recordAfterReplace = replaceAll(record);
  populateLogRecord(logRecord, recordAfterReplace);

  collector.emit(logRecord);
}
 
Example 4
Source File: SignUtils.java    From disconf with Apache License 2.0 5 votes vote down vote up
/**
 * 生成token ,使用 UUID + 手机生成
 *
 * @return
 */
public static String createToken(String phone) {

    String uuid = UUID.randomUUID().toString();

    String data = DigestUtils.shaHex(uuid + phone);
    return data;
}
 
Example 5
Source File: AbstractTest.java    From git-code-format-maven-plugin with MIT License 5 votes vote down vote up
protected String sha1(String sourceName) {
  try (InputStream inputStream =
      Files.newInputStream(resolveRelativelyToProjectRoot(sourceName))) {
    return DigestUtils.shaHex(inputStream);
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}
 
Example 6
Source File: SignUtils.java    From disconf with Apache License 2.0 5 votes vote down vote up
/**
 * 生成token ,使用 UUID + 手机生成
 *
 * @return
 */
public static String createToken(String phone) {

    String uuid = UUID.randomUUID().toString();

    String data = DigestUtils.shaHex(uuid + phone);
    return data;
}
 
Example 7
Source File: RedisPriorityScheduler.java    From webmagic with Apache License 2.0 5 votes vote down vote up
private Request getExtrasInItem(Jedis jedis, String url, Task task)
{
    String key      = getItemKey(task);
    String field    = DigestUtils.shaHex(url);
    byte[] bytes    = jedis.hget(key.getBytes(), field.getBytes());
    if(bytes != null)
        return JSON.parseObject(new String(bytes), Request.class);
    return new Request(url);
}
 
Example 8
Source File: PuppetNodesProcess.java    From primecloud-controller with GNU General Public License v2.0 5 votes vote down vote up
protected String getFileDigest(File file, String encoding) {
    if (!file.exists()) {
        return null;
    }
    try {
        String content = FileUtils.readFileToString(file, encoding);
        return DigestUtils.shaHex(content);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 9
Source File: LoginController.java    From Library-Assistant with Apache License 2.0 5 votes vote down vote up
@FXML
private void handleLoginButtonAction(ActionEvent event) {
    String uname = StringUtils.trimToEmpty(username.getText());
    String pword = DigestUtils.shaHex(password.getText());

    if (uname.equals(preference.getUsername()) && pword.equals(preference.getPassword())) {
        closeStage();
        loadMain();
        LOGGER.log(Level.INFO, "User successfully logged in {}", uname);
    }
    else {
        username.getStyleClass().add("wrong-credentials");
        password.getStyleClass().add("wrong-credentials");
    }
}
 
Example 10
Source File: RedisPriorityScheduler.java    From webmagic with Apache License 2.0 5 votes vote down vote up
private void setExtrasInItem(Jedis jedis,Request request, Task task)
{
    if(request.getExtras() != null)
    {
        String field = DigestUtils.shaHex(request.getUrl());
        String value = JSON.toJSONString(request);
        jedis.hset(getItemKey(task), field, value);
    }
}
 
Example 11
Source File: FileDigitalSignature.java    From common_gui_tools with Apache License 2.0 4 votes vote down vote up
/**
 * 计算文件数字签名-SHA1.
 */
public static String digitalFileSha1(File file) throws IOException {
    return DigestUtils.shaHex(new FileInputStream(file));
}
 
Example 12
Source File: EntityGraphMapper.java    From atlas with Apache License 2.0 4 votes vote down vote up
private Object mapPrimitiveValue(AtlasVertex vertex, AtlasAttribute attribute, Object valueFromEntity, boolean isDeletedEntity) {
    boolean isIndexableStrAttr = attribute.getAttributeDef().getIsIndexable() && attribute.getAttributeType() instanceof AtlasBuiltInTypes.AtlasStringType;

    Object ret = valueFromEntity;

    // Janus bug, when an indexed string attribute has a value longer than a certain length then the reverse indexed key generated by JanusGraph
    // exceeds the HBase row length's hard limit (Short.MAX). This trimming and hashing procedure is to circumvent that limitation
    if (ret != null && isIndexableStrAttr) {
        String value = ret.toString();

        if (value.length() > INDEXED_STR_SAFE_LEN) {
            RequestContext requestContext = RequestContext.get();

            final int trimmedLength;

            if (requestContext.getAttemptCount() <= 1) { // if this is the first attempt, try saving as it is; trim on retry
                trimmedLength = value.length();
            } else if (requestContext.getAttemptCount() >= requestContext.getMaxAttempts()) { // if this is the last attempt, set to 'safe_len'
                trimmedLength = INDEXED_STR_SAFE_LEN;
            } else if (requestContext.getAttemptCount() == 2) { // based on experimentation, string length of 4 times 'safe_len' succeeds
                trimmedLength = Math.min(4 * INDEXED_STR_SAFE_LEN, value.length());
            } else if (requestContext.getAttemptCount() == 3) { // if length of 4 times 'safe_len' failed, try twice 'safe_len'
                trimmedLength = Math.min(2 * INDEXED_STR_SAFE_LEN, value.length());
            } else { // if twice the 'safe_len' failed, trim to 'safe_len'
                trimmedLength = INDEXED_STR_SAFE_LEN;
            }

            if (trimmedLength < value.length()) {
                LOG.warn("Length of indexed attribute {} is {} characters, longer than safe-limit {}; trimming to {} - attempt #{}", attribute.getQualifiedName(), value.length(), INDEXED_STR_SAFE_LEN, trimmedLength, requestContext.getAttemptCount());

                String checksumSuffix = ":" + DigestUtils.shaHex(value); // Storing SHA checksum in case verification is needed after retrieval

                ret = value.substring(0, trimmedLength - checksumSuffix.length()) + checksumSuffix;
            } else {
                LOG.warn("Length of indexed attribute {} is {} characters, longer than safe-limit {}", attribute.getQualifiedName(), value.length(), INDEXED_STR_SAFE_LEN);
            }
        }
    }

    AtlasGraphUtilsV2.setEncodedProperty(vertex, attribute.getVertexPropertyName(), ret);

    String uniqPropName = attribute != null ? attribute.getVertexUniquePropertyName() : null;

    if (uniqPropName != null) {
        if (isDeletedEntity || AtlasGraphUtilsV2.getState(vertex) == DELETED) {
            vertex.removeProperty(uniqPropName);
        } else {
            AtlasGraphUtilsV2.setEncodedProperty(vertex, uniqPropName, ret);
        }
    }

    return ret;
}
 
Example 13
Source File: DebugSHAEncoder.java    From webcurator with Apache License 2.0 4 votes vote down vote up
public String encodePassword(String rawPass, Object salt) {
    String saltedPass = mergePasswordAndSalt(rawPass, salt, false);
    
    System.out.println("mergedPasswordAndSalt: ["+saltedPass+"]");

    if (!getEncodeHashAsBase64()) {
        System.out.println("Not Doing base 64");
        return DigestUtils.shaHex(saltedPass);
    }

    
    byte[] encoded = Base64.encodeBase64(DigestUtils.sha(saltedPass));
    System.out.println("encodedPass: ["+new String(encoded)+"]");

    return new String(encoded);
}
 
Example 14
Source File: JobControlCompiler.java    From spork with Apache License 2.0 4 votes vote down vote up
private static Path getFromCache(PigContext pigContext,
        Configuration conf,
        URL url) throws IOException {
    InputStream is1 = null;
    InputStream is2 = null;
    OutputStream os = null;

    try {
        Path stagingDir = getCacheStagingDir(conf);
        String filename = FilenameUtils.getName(url.getPath());

        is1 = url.openStream();
        String checksum = DigestUtils.shaHex(is1);
        FileSystem fs = FileSystem.get(conf);
        Path cacheDir = new Path(stagingDir, checksum);
        Path cacheFile = new Path(cacheDir, filename);
        if (fs.exists(cacheFile)) {
            log.debug("Found " + url + " in jar cache at "+ cacheDir);
            long curTime = System.currentTimeMillis();
            fs.setTimes(cacheFile, -1, curTime);
            return cacheFile;
        }
        log.info("Url "+ url + " was not found in jarcache at "+ cacheDir);
        // attempt to copy to cache else return null
        fs.mkdirs(cacheDir, FileLocalizer.OWNER_ONLY_PERMS);
        is2 = url.openStream();
        os = FileSystem.create(fs, cacheFile, FileLocalizer.OWNER_ONLY_PERMS);
        IOUtils.copyBytes(is2, os, 4096, true);

        return cacheFile;

    } catch (IOException ioe) {
        log.info("Unable to retrieve jar from jar cache ", ioe);
        return null;
    } finally {
        org.apache.commons.io.IOUtils.closeQuietly(is1);
        org.apache.commons.io.IOUtils.closeQuietly(is2);
        // IOUtils should not close stream to HDFS quietly
        if (os != null) {
            os.close();
        }
    }
}
 
Example 15
Source File: WebhookService.java    From webanno with Apache License 2.0 4 votes vote down vote up
@TransactionalEventListener(fallbackExecution = true)
@Async
public void onApplicationEvent(ApplicationEvent aEvent)
{
    String topic = EVENT_TOPICS.get(aEvent.getClass());
    if (topic == null) {
        return;
    }
    
    Object message;
    switch (topic) {
    case PROJECT_STATE:
        message = new ProjectStateChangeMessage((ProjectStateChangedEvent) aEvent);
        break;
    case DOCUMENT_STATE:
        message = new DocumentStateChangeMessage((DocumentStateChangedEvent) aEvent);
        break;
    case ANNOTATION_STATE:
        message = new AnnotationStateChangeMessage((AnnotationStateChangeEvent) aEvent);
        break;
    default:
        return;
    }
    
    for (Webhook hook : configuration.getGlobalHooks()) {
        if (!hook.isEnabled() || !hook.getTopics().contains(topic)) {
            continue;
        }

        try {
            // Configure rest template without SSL certification check if that is disabled.
            RestTemplate restTemplate;
            if (hook.isVerifyCertificates()) {
                restTemplate = restTemplateBuilder.build();
            }
            else {
                restTemplate = restTemplateBuilder
                        .requestFactory(this::getNonValidatingRequestFactory).build();
            }
            
            HttpHeaders requestHeaders = new HttpHeaders();
            requestHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
            requestHeaders.set(X_AERO_NOTIFICATION, topic);
            
            // If a secret is set, then add a digest header that allows the client to verify
            // the message integrity
            String json = JSONUtil.toJsonString(message);
            if (isNotBlank(hook.getSecret())) {
                String digest = DigestUtils.shaHex(hook.getSecret() + json);
                requestHeaders.set(X_AERO_SIGNATURE, digest);
            }

            HttpEntity<?> httpEntity = new HttpEntity<Object>(json, requestHeaders);
            restTemplate.postForEntity(hook.getUrl(), httpEntity, Void.class);
        }
        catch (Exception e) {
            log.error("Unable to invoke webhook [{}]", hook, e);
        }
    }
}
 
Example 16
Source File: ShaEncrypt.java    From ralasafe with MIT License 4 votes vote down vote up
public String encrypt( String rawTxt ) {
	if( rawTxt==null ) {
		return null;
	}
	return DigestUtils.shaHex( rawTxt );
}
 
Example 17
Source File: TenantIdToDbName.java    From secure-data-service with Apache License 2.0 4 votes vote down vote up
public String load(String tenant) {
    return DigestUtils.shaHex(tenant);
}
 
Example 18
Source File: ContentDispositionParserTest.java    From email-mime-parser with Apache License 2.0 4 votes vote down vote up
private String generateCheckSum(String plainText) throws IOException {
	return DigestUtils.shaHex(plainText);
}
 
Example 19
Source File: ParserTest.java    From email-mime-parser with Apache License 2.0 4 votes vote down vote up
private String generateCheckSum(String filename) throws IOException {
	return DigestUtils.shaHex(filename);
}
 
Example 20
Source File: ShaPasswordEncoder.java    From nextreports-server with Apache License 2.0 4 votes vote down vote up
public String encode(String plainPassword) {
    return DigestUtils.shaHex("" + plainPassword);
}