public String hash() { return cpf.getProvider(ChecksumType.SHA1).putFile(path).checksumAsString(); }
@Override public ChecksumProvider putBytes(byte[] byteArray, int offset, int length) { verifyState(); /* provide bounds exception specified by method documentation */ @SuppressWarnings("unused") final byte start = byteArray[offset]; @SuppressWarnings("unused") final byte end = byteArray[offset + length - 1]; size += length; return this; }
/** * @see ChecksumProvider#putBytes(byte[]) */ public ChecksumProvider putBytes(byte[] byteArray) { return this.putBytes(byteArray, 0, byteArray.length); }
/** * Calculates the local sha1 for a file. * @param file a local file * @return the file's SHA-1 hexadecimal digest */ public String sha1(File file) { ChecksumProviderFactory cpf = new ChecksumProviderFactoryImpl(); return cpf.getProvider(ChecksumType.SHA1).putFile( file.getAbsolutePath()).checksumAsString(); }
/** * @see ChecksumProviderFactory#getProvider(ChecksumType) */ public ChecksumProvider getProvider(ChecksumType checksumType) { // Dumb implementation for now // TODO: remove the switch statement switch (checksumType) { case FILE_SIZE: return new FileSizeChecksumProviderImpl(); case ADLER32: return new Adler32ChecksumProviderImpl(); case CRC32: return new CRC32ChecksumProviderImpl(); case MD5: return new MD5ChecksumProviderImpl(); case MURMUR32: return new Murmur32ChecksumProviderImpl(); case MURMUR128: return new Murmur128ChecksumProviderImpl(); case SHA1: default: return new SHA1ChecksumProviderImpl(); } }
public OriginalFileI getFile(ServiceFactory sf) { OriginalFile file = null; ChecksumProviderFactory cpf = new ChecksumProviderFactoryImpl(); try { final byte[] buf = FileUtils.readFileToByteArray(source); final String sha1 = cpf.getProvider(ChecksumType.SHA1) .putBytes(buf).checksumAsString(); log.debug("Loading script: " + sha1); Parameters p = new Parameters(); p.addString("hash", sha1); p.addString("name", getName()); List<OriginalFile> files = loadScripts(sf, p); if (files.size() < 1) { return null; } else { if (files.size() > 1) { log.warn("Multiple scripts found: " + files); } file = files.get(0); } } catch (Exception e) { // pass in order to throw log.warn(e.toString()); // slf4j migration: toString() } if (file == null) { throw new InternalException("Failed to find script"); } else { return new OriginalFileI(file.getId(), false); } }
protected String passwordDigest(Long userId, String clearText, boolean salt) { if (clearText == null) { throw new ApiUsageException("Value for digesting may not be null"); } byte[] bytes = clearText.getBytes(encoding); // If salting is activated, prepend the salt. if (userId != null && salt) { byte[] saltedBytes = ByteBuffer.allocate(8).putLong(userId).array(); byte[] newValue = new byte[saltedBytes.length+bytes.length]; System.arraycopy(saltedBytes, 0, newValue, 0, saltedBytes.length); System.arraycopy(bytes, 0, newValue, saltedBytes.length, bytes.length); bytes = newValue; } String hashedText = null; ChecksumProviderFactory cpf = new ChecksumProviderFactoryImpl(); try { bytes = cpf.getProvider(ChecksumType.MD5).putBytes(bytes) .checksumAsBytes(); bytes = Base64.encodeBase64(bytes); hashedText = new String(bytes); } catch (Exception e) { log.error("Could not hash password", e); } if (hashedText == null) { throw new InternalException("Failed to obtain digest."); } return hashedText; }
/** * Make the file, this is a temporary file which will be changed when the * script is validated. * * @param script * script. * @return OriginalFile tempfile.. * @throws ServerError */ private OriginalFile makeFile(final String path, final String script, Ice.Current current) throws ServerError { OriginalFile file = new OriginalFile(); file.setName(FilenameUtils.getName(path)); file.setPath(FilenameUtils.getFullPath(path)); file.setSize((long) script.getBytes().length); file.setHasher(new ChecksumAlgorithm("SHA1-160")); file.setHash(cpf.getProvider(ChecksumType.SHA1) .putBytes(script.getBytes()).checksumAsString()); scripts.setMimetype(file); return updateFile(file, current); }
public String hash() { if (hash == null && this.checksumProvider != null) { hash = this.checksumProvider .putFile(file.getPath()) .checksumAsString(); } return hash; }
/** * "Transfer" files by soft-linking them into place. This method is likely * re-usable for other general "linking" strategies by overriding * {@link #createProcessBuilder(File, File)} and the other protected methods here. */ public String transfer(TransferState state) throws IOException, ServerError { RawFileStorePrx rawFileStore = start(state); try { final OriginalFile root = state.getRootFile(); final OriginalFile ofile = state.getOriginalFile(); final File location = getLocalLocation(root, ofile); final File file = state.getFile(); final long length = state.getLength(); final ChecksumProvider cp = state.getChecksumProvider(); state.uploadStarted(); checkLocation(location, rawFileStore); // closes rawFileStore state.closeUploader(); exec(file, location); checkTarget(location, state); cp.putFile(file.getAbsolutePath()); state.stop(length); state.uploadBytes(length); return finish(state, length); } finally { state.closeUploader(); } }
/** * @see ChecksumProvider#putBytes(byte[], int, int) */ public ChecksumProvider putBytes(byte[] byteArray, int offset, int length) { this.verifyState(this.hashBytes, this.hashString); this.hasher.putBytes(byteArray, offset, length); return this; }
/** * @see ChecksumProvider#checksumAsBytes() */ public byte[] checksumAsBytes() { this.hashBytes = Optional.of(this.pickChecksum().asBytes()); return this.hashBytes.get(); }
protected String checksum(String filename, ChecksumAlgorithm checksumAlgorithm) { final ChecksumProviderFactory checksumProviderFactory = new ChecksumProviderFactoryImpl(); final ChecksumProvider cp = checksumProviderFactory.getProvider( ChecksumAlgorithmMapper.getChecksumType(checksumAlgorithm)); cp.putFile(filename); return cp.checksumAsString(); }
public List<Long> verifyChecksums(List<Long> ids, Current __current) throws ServerError { /* set up an invocation context in which the group is set to -1, for "all groups" */ final Current allGroupsCurrent = makeAdjustedCurrent(__current); allGroupsCurrent.ctx = new HashMap<String, String>(__current.ctx); allGroupsCurrent.ctx.put(omero.constants.GROUP.value, "-1"); /* verify the checksum of the specified files that are in this repository */ final List<Long> mismatchFiles = new ArrayList<Long>(); for (final long id : repositoryDao.filterFilesByRepository(getRepoUuid(), ids, allGroupsCurrent)) { /* get one of the files */ final OriginalFile file = repositoryDao.getOriginalFileWithHasher(id, allGroupsCurrent); final FsFile fsPath = new FsFile(file.getPath() + file.getName()); final String osPath = serverPaths.getServerFileFromFsFile(fsPath).getAbsolutePath(); /* check the file's checksum */ final ome.model.enums.ChecksumAlgorithm hasher = file.getHasher(); final String hash = file.getHash(); if (hasher != null && hash != null) { /* has a valid checksum, so check it */ final ChecksumProvider fromProvider = checksumProviderFactory.getProvider(ChecksumAlgorithmMapper.getChecksumType(hasher)); fromProvider.putFile(osPath); if (!fromProvider.checksumAsString().equalsIgnoreCase(hash)) { mismatchFiles.add(id); } } } return mismatchFiles; }
@Override public ChecksumProvider putBytes(byte[] byteArray) { verifyState(); size += byteArray.length; return this; }
/** * @see ChecksumProvider#putBytes(ByteBuffer) */ public ChecksumProvider putBytes(ByteBuffer byteBuffer) { this.verifyState(this.hashBytes, this.hashString); if (byteBuffer.hasArray()) { this.hasher.putBytes(byteBuffer.array(), 0, byteBuffer.limit()); return this; } else { throw new IllegalArgumentException("Supplied ByteBuffer has " + "inaccessible array."); } }
/** * @see ChecksumProvider#checksumAsString() */ public String checksumAsString() { this.hashString = Optional.of(this.pickChecksum().toString()); return this.hashString.get(); }
@Override public ChecksumProvider putFile(String filePath) { verifyState(); size = new File(filePath).length(); return this; }
/** * @see ChecksumProvider#putFile(String) */ public ChecksumProvider putFile(String filePath) { this.verifyState(this.hashBytes, this.hashString); try { this.hashCode = Optional.of( Files.hash(new File(filePath), this.hashFunction)); return this; } catch (IOException io) { throw new RuntimeException(io); } }
@Override public ChecksumProvider putBytes(ByteBuffer byteBuffer) { verifyState(); if (!byteBuffer.hasArray()) { throw new IllegalArgumentException("Supplied ByteBuffer has " + "inaccessible array."); } size += byteBuffer.limit() - byteBuffer.position(); return this; }