@Override public String invoke(File f, VirtualChannel channel) throws IOException { return Util.getDigestOf(reading(f)); } }
@Nonnull public static String getDigestOf(@Nonnull String text) { try { return getDigestOf(new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8))); } catch (IOException e) { throw new Error(e); } }
/** * Computes the MD5 digest of a file. * @param file a file * @return a 32-character string * @throws IOException in case reading fails * @since 1.525 */ @Nonnull public static String getDigestOf(@Nonnull File file) throws IOException { // Note: getDigestOf() closes the input stream. return getDigestOf(Files.newInputStream(fileToPath(file))); }
/** * Returns the unique identifier of this Jenkins that has been historically used to identify * this Jenkins to the outside world. * * <p> * This form of identifier is weak in that it can be impersonated by others. See * https://wiki.jenkins-ci.org/display/JENKINS/Instance+Identity for more modern form of instance ID * that can be challenged and verified. * * @since 1.498 */ @SuppressWarnings("deprecation") public String getLegacyInstanceId() { return Util.getDigestOf(getSecretKey()); }
context.setAttribute("version",ver); VERSION_HASH = Util.getDigestOf(ver).substring(0, 8); SESSION_HASH = Util.getDigestOf(ver+System.currentTimeMillis()).substring(0, 8);
/** * If {@code jenkins.exe} is old compared to our copy, * schedule an overwrite (except that since it's currently running, * we can only do it when Jenkins restarts next time.) */ private void updateJenkinsExeIfNeeded() { try { File baseDir = getBaseDir(); URL exe = getClass().getResource("/windows-service/jenkins.exe"); String ourCopy = Util.getDigestOf(exe.openStream()); for (String name : new String[]{"hudson.exe","jenkins.exe"}) { try { File currentCopy = new File(baseDir,name); if(!currentCopy.exists()) continue; String curCopy = new FilePath(currentCopy).digest(); if(ourCopy.equals(curCopy)) continue; // identical File stage = new File(baseDir,name+".new"); FileUtils.copyURLToFile(exe,stage); Kernel32.INSTANCE.MoveFileExA(stage.getAbsolutePath(),currentCopy.getAbsolutePath(),MOVEFILE_DELAY_UNTIL_REBOOT|MOVEFILE_REPLACE_EXISTING); LOGGER.info("Scheduled a replacement of "+name); } catch (IOException e) { LOGGER.log(Level.SEVERE, "Failed to replace "+name,e); } catch (InterruptedException e) { } } } catch (IOException e) { LOGGER.log(Level.SEVERE, "Failed to replace jenkins.exe",e); } }
private void addLegacyToken(@Nonnull Secret legacyToken, boolean migrationFromExistingLegacy) { String tokenUserUseNormally = Util.getDigestOf(legacyToken.getPlainText()); String secretValueHashed = this.plainSecretToHashInHex(tokenUserUseNormally); HashValue hashValue = new HashValue(LEGACY_VERSION, secretValueHashed); HashedToken token = HashedToken.buildNewFromLegacy(hashValue, migrationFromExistingLegacy); this.addToken(token); }
@Nonnull @Restricted(NoExternalUse.class) /*package*/ String getApiTokenInsecure() { if(apiToken == null){ return Messages.ApiTokenProperty_NoLegacyToken(); } String p = apiToken.getPlainText(); if (p.equals(Util.getDigestOf(Jenkins.getInstance().getSecretKey()+":"+user.getId()))) { // if the current token is the initial value created by pre SECURITY-49 Jenkins, we can't use that. // force using the newer value apiToken = Secret.fromString(p=API_KEY_SEED.mac(user.getId())); } return Util.getDigestOf(p); }
public static @CheckForNull User isConnectingUsingApiToken(String username, String tokenValue){ User user = User.getById(username, false); if(user == null){ ApiTokenPropertyConfiguration apiTokenConfiguration = GlobalConfiguration.all().getInstance(ApiTokenPropertyConfiguration.class); if(apiTokenConfiguration.isTokenGenerationOnCreationEnabled()){ String generatedTokenOnCreation = Util.getDigestOf(ApiTokenProperty.API_KEY_SEED.mac(username)); boolean areTokenEqual = MessageDigest.isEqual( generatedTokenOnCreation.getBytes(StandardCharsets.US_ASCII), tokenValue.getBytes(StandardCharsets.US_ASCII) ); if(areTokenEqual){ // directly return the user freshly created // and no need to check its token as the generated token // will be the same as the one we checked just above return User.getById(username, true); } } }else{ ApiTokenProperty t = user.getProperty(ApiTokenProperty.class); if (t!=null && t.matchesPassword(tokenValue)) { return user; } } return null; } }
InputStream fingerprintInput = baseFile.open(); try { rsp.forward(Jenkins.getInstance().getFingerprint(Util.getDigestOf(fingerprintInput)), "/", req); } finally { fingerprintInput.close();
/** * Do a finger-print check. */ @RequirePOST public void doDoFingerprintCheck( StaplerRequest req, StaplerResponse rsp ) throws IOException, ServletException { // Parse the request try (MultipartFormDataParser p = new MultipartFormDataParser(req)) { if (isUseCrumbs() && !getCrumbIssuer().validateCrumb(req, p)) { // TODO investigate whether this check can be removed rsp.sendError(HttpServletResponse.SC_FORBIDDEN, "No crumb found"); } rsp.sendRedirect2(req.getContextPath()+"/fingerprint/"+ Util.getDigestOf(p.getFileItem("name").getInputStream())+'/'); } }
public static String getDigestOf(String text) { try { return getDigestOf(new ByteArrayInputStream(text.getBytes("UTF-8"))); } catch (IOException e) { throw new Error(e); } }
public static String getDigestOf(String text) { try { return getDigestOf(new ByteArrayInputStream(text.getBytes("UTF-8"))); } catch (IOException e) { throw new Error(e); } }
/** * Gets the 8 character-wide hash code for this combination */ public String digest() { return Util.getDigestOf(toString()); }
/** * Gets the 8 character-wide hash code for this combination */ public String digest() { return Util.getDigestOf(toString()); }
/** * Gets the 8 character-wide hash code for this combination */ public String digest() { return Util.getDigestOf(toString()); }
@Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String path = req.getServletPath(); String d = Util.getDigestOf(path); File cache = new File(cacheFolder, d); if(!cache.exists()) { URL url = new URL("http://hudson-ci.org/" + path); FileUtils.copyURLToFile(url,cache); } resp.setContentType(getMimeType(path)); IOUtils.copy(cache,resp.getOutputStream()); }
public static String getInstanceId() { try { if( _id == null ) { _id = Util.getDigestOf( new ByteArrayInputStream(InstanceIdentity.get().getPublic().getEncoded())); } } catch (IOException e) { LOG.error("Could not get Jenkins instance ID."); _id = ""; } return _id; }
@Override public SlaveInstaller createIfApplicable(Channel c) throws IOException, InterruptedException { if (c.call(new HasSystemd())) { RSAPublicKey key = id.getPublic(); String instanceId = Util.getDigestOf(new String(Base64.encodeBase64(key.getEncoded()))).substring(0,8); return new SystemdSlaveInstaller(instanceId); } return null; }
@Nonnull @Restricted(NoExternalUse.class) /*package*/ String getApiTokenInsecure() { String p = apiToken.getPlainText(); if (p.equals(Util.getDigestOf(Jenkins.getInstance().getSecretKey()+":"+user.getId()))) { // if the current token is the initial value created by pre SECURITY-49 Jenkins, we can't use that. // force using the newer value apiToken = Secret.fromString(p=API_KEY_SEED.mac(user.getId())); } return Util.getDigestOf(p); }