Skip to content

Commit

Permalink
better soln, handle padding
Browse files Browse the repository at this point in the history
  • Loading branch information
cboettig committed Jan 21, 2025
1 parent 95d70b4 commit a3ffbe4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
29 changes: 25 additions & 4 deletions R/format_identifiers.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@


# ssb is https://github.com/ssbc/docs/blob/master/ssb/linking.md
# sri is https://www.w3.org/TR/SRI/
# ni is https://datatracker.ietf.org/doc/html/rfc6920
# hashuri is https://github.com/hash-uri/hash-uri
# magnet is https://magnet-uri.sourceforge.net/

as_hashuri <- function(id){

Expand Down Expand Up @@ -86,7 +89,7 @@ is_hash <- function(x, type = c("hashuri", "magnet", "sri", "ssb", "ni")){
grepl(pattern, x)
}


# SRI: https://www.w3.org/TR/SRI/
# sri <- "sha256-lBIyWDHasiruvdZ0tutTumt73QS7maTbsh3f9kYofjc="
from_sri <- function(x){
hash <- gsub(sri_regex, "\\2", x)
Expand All @@ -96,6 +99,7 @@ from_sri <- function(x){

}

# https://github.com/ssbc/docs/blob/master/ssb/linking.md
# ssb <- "&lBIyWDHasiruvdZ0tutTumt73QS7maTbsh3f9kYofjc=.sha256"
from_ssb <- function(x){
hash <- gsub(ssb_regex, "\\1", x)
Expand All @@ -105,14 +109,31 @@ from_ssb <- function(x){

}



## NAMED INFO: https://datatracker.ietf.org/doc/html/rfc6920

## note that named info doesn't have the trailing '='
## argh this should be base64url encoded (that's URL-encoded!), and without trailing `=`, by spec
# ni <- "ni:///sha256;lBIyWDHasiruvdZ0tutTumt73QS7maTbsh3f9kYofjc"
fix_padding <- function (text)
{
text <- gsub("[\r\n]", "", text)[[1]]
mod <- nchar(text)%%4
if (mod > 0) {
padding <- paste(rep("=", (4 - mod)), collapse = "")
text <- paste0(text, padding)
}
text
}
from_ni <- function(x){


hash <- gsub(paste0("^ni:///([a-zA-Z0-9]+);", base64_regex), "\\2", x)
algo <- gsub(paste0("^ni:///", algo_regex, ";", hash), "\\1", x)
hex <- paste0(jsonlite::base64_dec((hash), collapse = "")
hash <- fix_padding(hash)
hex <- paste0(openssl::base64_decode(hash), collapse = "")

paste0("hash://", algo, "/", hex)

}
Expand Down
1 change: 1 addition & 0 deletions contentid.Rproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Version: 1.0
ProjectId: 7ef72c19-510e-4f6f-8b5e-3d629b674aab

RestoreWorkspace: Default
SaveWorkspace: Default
Expand Down

0 comments on commit a3ffbe4

Please sign in to comment.